Beanutils Use summary Setproperties__util tool class

Source: Internet
Author: User

1, Beanutils is divided into 4 packages:

Org.apache.commons.beanutils

Org.apache.commons.beanutils.converters

Org.apache.commons.beanutils.locale

Org.apache.commons.beanutils.locale.converters

The above two are the default implementations of Beanutils, which have no processing for localization, which can improve execution efficiency.

However, if your program is required for localization, it is safer to use the following 2 packages.

Org.apache.commons.beanutils

This package mainly provides tool classes for manipulating JavaBean, and Jakarta-common-beanutils's main functions are implemented in this package.

2, Beanutils can directly get and set the value of a property. It divides the property into 3 different types:

simple--simple types, such as STIRNG, Int ...

indexed--index type, array, arrayList ...

maped--This needless to say also should know, is refers to map, such as HashMap ...

accessing different types of data can call functions GetProperty and setproperty directly. GetProperty has only 2 parameters, the first is the JavaBean object, the second is the name of the property to be manipulated, the SetProperty provides three parameters, the first is the JavaBean object, the second is the name of the property to manipulate, and the third is the specific value to set
public class Company {private String name, private HashMap address = new HashMap (); private string[] otherinfo; private ArrayList product; Private ArrayList employee; Private HASHMAP telephone; public [] {} public String GetName () {return name.} public void SetName (String name) {this.name = name; String getaddress (String type) {return Address.get (type). ToString ();} public void Setaddress (string type, string address {this.address.put (type,address);} public string[] Getotherinfo () {return otherinfo;} public void Setotherinfo (String [] Otherinfo {this.otherinfo = otherinfo;} public ArrayList getproduct () {return product;} public void Setproduct (ARR Aylist product) {this.product = product;} public ArrayList GetEmployee () {return employee;} public void Setemployee (Ar Raylist Employee} {this.employee = employee; public HashMap Gettelephone () {return telephone;} public void Settelepho NE (HashMap telephone) {this.telephone = telephone;}}

(1) Simple

For simple types, parameter two is directly a property name

System.out.println (Beanutils.getproperty (c, "name"));

(2) Map

For the map type, it needs to be in the form of a property name (key value)

System.out.println (Beanutils.getproperty (C, "Address (A2)"));

HashMap am = new HashMap ();

Am.put ("1", "234-222-1222211");

Am.put ("2", "021-086-1232323");

Beanutils.setproperty (c, "telephone", am);

System.out.println (Beanutils.getproperty (c, "Telephone (2)"));

Map types can also be used Beanutils.getmappedproperty (c, "address", "A2");

(3) Index

For indexed, the property name [index value], note that this can be done in the same way for ArrayList and arrays.

System.out.println (Beanutils.getproperty (C, "otherinfo[2]"));

Beanutils.setproperty (C, "product[1]", "NOTES SERVER");

System.out.println (Beanutils.getproperty (C, "product[1]"));

Index types can also be used with Beanutils.getindexedproperty (c, "product", 1);

(4) Nest 3 kinds can also be used together.

System.out.println (Beanutils.getproperty (c, "Employee[1].name"));

3. Bean copy function

Company C2 = new company ();

Beanutils.copyproperties (C2, C);

But this copy is a shallow copy, and the same property of the 2 bean that is copied may have the same object's ref, which should be used with care, especially if the attribute is a custom class

4, Beanutils and Propertyutils

These two classes have almost the same functionality, the only difference being that beanutils is a type conversion when assigning a value to a bean. For example, when Copyproperty is the same as the name of the property, even if the type is different, beanutils can also be copy, and Propertybean may be an error.

For the above example, create a new Company2 class where the code, like company, simply changes Otherinfo from string[to string.

Company C = init ();

Company2 C2 = new Company2 ();

Beanutils.copyproperties (C2,C);

Propertyutils.copyproperties (C2,C); This sentence will be an error.

System.out.println (C2.getotherinfo ());

Of course, the type of the attribute with the same name between 2 beans must be converted, otherwise the error will be the same as beanutils.

If you implement the Org.apache.commons.beanutils.Converter interface, you can customize the transformation between types.

Because do not do type transformation, use Propertyutils to be able to improve greatly in speed.

In addition, there is a benefit to not converting the type, such as the following code:

Test data type Convert

ArrayList A1 = Beanutils.getproperty (C, "product"); Beanutils returns a string.

System.out.println ("--" + Beanutils.getproperty (c, "product")); be converted to string directly after removal

ArrayList a = (ArrayList) propertyutils.getproperty (c, "product");//propertyutils returns object.

System.out.println ("--" + a.get (1));

You cannot return an object with Beanutils (unless you write a converter yourself), it automatically converts the type, and then returns a string. If you want to return to a Java class or custom class, use the Propertyutils

5, Utils class

All Xxxutils classes provide static methods that can be invoked directly, and their main implementations are in the corresponding Xxxutilsbean:

Beanutils--> Beanutilsbean

Convertutils--> Convertutilsbean

Propertyutils--> Propertyutilsbean

(1)propertyutils, gets the class type of the property

public static Class Getpropertytype (Object Bean, String name)

(2)constructorutils, dynamically creating objects

public static Object Invokeconstructor (Class Klass, Object Arg)

(3)methodutils, dynamic invocation method

Methodutils.invokemethod (Bean, methodname, parameter);

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.