Beanwrapper is a tool that spring provides to manipulate the JavaBean property, which you can use to directly modify the properties of an object.
For the operation of Bean properties, it is well known that the following tools are the main classes:
1.Apache of Beanutils and Propertyutils
2.cglib of Beanmap and Beancopier
3.spring of Beanutils
The benefits of Beanwrapper are:
1. Support Setting nested properties
2. Support for type conversions of attribute values (set Conversionservice)
Here is an example of a wrapper object using Beanwrapper:
Packagecom.glmapper.web.test;ImportOrg.springframework.beans.BeanWrapper;Importorg.springframework.beans.PropertyAccessorFactory;ImportOrg.springframework.beans.PropertyValue; Public classBeanwrappertest { Public Static voidMain (string[] args) {User User=NewUser (); //encapsulates a user object into Beanwrapper via PropertyaccessorfactoryBeanwrapper bw=propertyaccessorfactory.forbeanpropertyaccess (user); //method One: Set the property value directlyBw.setpropertyvalue ("UserName", "Zhang San"); //mode two: set by PropertyValuePropertyValue pv=NewPropertyValue ("UserName", "John Doe"); Bw.setpropertyvalue (PV); System.out.println (User.getusername ()); }}//a user classclassuser{PrivateString UserName; PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; }}
In spring, the Httpservletbean property setting is done in this way.
Beanwrapper in spring