Beanutils research experiences

Source: Internet
Author: User

Beanutils research experiences

Beanutils uses magic reflection technology to implement a lot of exaggerated and useful functions, which are hard to imagine in the C/C ++ era. No matter who the project is, it will always be used in a day. I have never realized it. I missed it the first time I saw it.

1. Dynamic getter and setter of the attribute

In the age when the framework was full, the getter and setter functions could not be executed on every things. Sometimes the attribute needs to be dynamically obtained based on the name, just like this:

 
Beanutils. getproperty (mybean, "Code ");

Beanutils is more powerful in accessing the attributes of embedded objects directly, as long as they are separated by commas.

 
Beanutils. getproperty (orderbean, "address. City ");

Beanutils of other class libraries are usually very simple and cannot access embedded objects. Therefore, they are often replaced with jakata's beanutils.

Beanutils also supports list and map attributes. The following syntax is used to obtain the name of the first customer in the customer list.

 
Beanutils. getproperty (orderbean, "MERs [1]. Name ");

Beanutils converts the string type to the real type of the bean attribute using the convertutils class, facilitating bean extraction from objects such as httpservletrequest, or outputting the bean to the page.

Propertyutils retains the original bean type.

2. beancompartor dynamic sorting

By reflection, we can dynamically set the attribute to which the bean is ordered, instead of making complicated condition judgments on the bean compare interface.List les = ...; // list of person objects collections. sort (events les, new beancomparator ("Age") ;}} to support composite sorting of multiple attributes, such as "order by lastname, firstname"

 
Arraylist sortfields = new arraylist (); sortfields. add (New beancomparator ("lastname"); sortfields. add (New beancomparator ("firstname"); comparatorchain multisort = new comparatorchain (sortfields); collections. sort (rows, multisort );

Comparatorchain belongs to the jakata commons-collections package.

If the age attribute is not of the normal type, the constructor needs to input a comparator object to sort the age variable.

3. converter converts the string in the request to the actual type object.

It is often necessary to retrieve values from objects such as request and resultset to be assigned to the bean.CodeEveryone is tired of writing, if you don't need the binding function of the MVC framework.

 
String A = request. getparameter ("A"); bean. Seta (a); string B = ....

Change

Mybean bean = ...; hashmap map = new hashmap (); enumeration names = request. getparameternames (); While (names. hasmoreelements () {string name = (string) names. nextelement (); map. put (name, request. getparametervalues (name);} beanutils. populate (bean, MAP );

The beanutils populate method or getproperty and setproperty Methods call convert for conversion.

Converter only supports some basic types, and even the java. util. date type is not supported. In addition, it is stupid because an exception is thrown when you encounter a unknown type.

For the date type, I have implemented a converter according to its sqldate type, and added a function to set the date format. To register this converter, you need the following statement:

Convertutilsbean convertutils = new convertutilsbean (); dateconverter = new dateconverter (); convertutils. register (dateconverter, date. class); // because you want to register converter, you cannot use beanutils's static method. You must create beanutilsbean instance beanutilsbean beanutils = new beanutilsbean (convertutils, new propertyutilsbean (); beanutils. setproperty (bean, name, value );! 4. Other functions: 4.1 propertyutils. When the attribute is collection and map, dynamic reading: Collection: Provides index {beanutils. getindexedproperty (orderbean, "items", 1 );

Or

 
Beanutils. getindexedproperty (orderbean, "items [1]");

Map: provides key value

 
Beanutils. getmappedproperty (orderbean, "items", "111"); // key-value goods_no = 111

Or

 
Beanutils. getmappedproperty (orderbean, "items (111 )");

4.2 propertyutils: obtains the class type of the attribute.

 
Public static class getpropertytype (Object bean, string name)

4.3 constructorutils: dynamically create objects

 
Public static object invokeconstructor (class Klass, object Arg)

4.4 dynaclass dynamic class

Comparatorchain belongs to the jakata commons-collections package. It is often necessary to retrieve values from objects such as request and resultset to be assigned to the bean. No one in the code below is tired of writing, if the binding function of the MVC framework is not needed. The beanutils populate method or getproperty and setproperty method call convert for conversion. Or 4.2 propertyutils, get the class type of the attribute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.