The Beanwrapper in spring

Source: Internet
Author: User

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

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.