Introspection-beanutils Toolkit

Source: Internet
Author: User

Apache has developed a set of APIS for operating JavaBean. This API takes into account many practical application scenarios. Therefore, in actual development, many programmers use this API to operate JavaBean, to simplify the compilation of program code.

Beanutils:

1) converts string to eight basic data types;

2) For other referenced data types, you must register the converter: convertutils. Register (converter, class );

public class Person {    private String name;    private String password;    private Integer age;    private Date birthday;    getter/setter}

 

Use beanutils to set attributes

@Testpublic void test1() throws Exception {    Person person = new Person();    BeanUtils.setProperty(person, "name", "zhangsan");    System.out.println(person.getName());}

 

Copying object attributes using beanutils (basic type)

@ Testpublic void Test2 () throws exception {string name = "zhangsan"; string Password = "123"; string age = "34"; person = new person (); beanutils. setproperty (person, "name", name); beanutils. setproperty (person, "password", password); beanutils. setproperty (person, "Age", age );// Automatic type conversion. Only eight basic data types are supported.System. Out. println (person );}

 

Copying object attributes using beanutils (non-basic type)

@ Testpublic void test3 () throws exception {string name = "zhangsan"; string Password = "123"; string age = "34"; string Birthday = "1980-09-05 "; person = new person (); beanutils. setproperty (person, "name", name); beanutils. setproperty (person, "password", password); beanutils. setproperty (person, "Age", age); // automatically converts the beanutils type. setproperty (person, "Birthday", birthday );//Type Conversion failedSystem. Out. println (person );}

To assign values for date to the birthday attribute, we register a date converter for beanutils.

The improved code is as follows:

@ Testpublic void test4 () throws exception {string name = "zhangsan"; string Password = "123"; string age = "34"; string Birthday = "1980-09-05 ";Convertutils. Register (New converter () {@ override public object convert (class type, object Value) {If (value = NULL) {return NULL;} If (! (Value instanceof string) {Throw new conversionexception ("only string type conversion is supported! ");} String STR = (string) value; If (null = STR | "". equals (Str. trim () {return NULL;} simpledateformat format = new simpledateformat ("yyyy-mm-dd"); try {return format. parse (STR);} catch (parseexception e) {Throw new conversionexception (e) ;}}, date. class);Person = new person (); beanutils. setproperty (person, "name", name); beanutils. setproperty (person, "password", password); beanutils. setproperty (person, "Age", age); // automatically converts the beanutils type. setproperty (person, "Birthday", birthday); system. out. println (person );}

 

Use the date converter provided by beanutils

@ Testpublic void test5 () throws exception {string name = "zhangsan"; string Password = "123"; string age = "34"; string Birthday = "1980-09-05"; convertutils. register (New datelocaleconverter (), date. class); person = new person (); beanutils. setproperty (person, "name", name); beanutils. setproperty (person, "password", password); beanutils. setproperty (person, "Age", age); // automatically converts the beanutils type. setproperty (person, "Birthday", birthday); system. out. println (person );}

 

Collect data to the map and copy the data to the specified object through beanutils.

@ Testpublic void test6 () throws exception {person = new person ();Convertutils. Register (New datelocaleconverter (), date. Class);Map <string, Object> map = new hashmap <string, Object> (); map. put ("name", "AA"); map. put ("password", "123"); map. put ("Age", 23); map. put ("Birthday", "1980-09-05"); // use the values in the map set to fill the bean attributes. // note: the attribute names must be consistent.Beanutils. populate (person, MAP );System. Out. println (person );}

 

Introspection-beanutils Toolkit

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.