6.4 Bean operation and one of the Beanwrapper classes

Source: Internet
Author: User

Due to the length of section 6.1, there are two articles to translate


6.4 Bean operations and Beanwrapper classes

The Org.springframework.beans package complies with the JavaBeans standard provided by Sun . A JavaBean needs to meet the following criteria : A class with a default parameterless constructor, and a naming convention, for example, if there is a property called bingomadness, then there must be a Setter method setbingomadness (..) And a getter method getbingomadness (). For more information on JavaBeans , refer to Sun 's website (Java.sun.com/products/javabeans).

InBeansa fairly important class in the package--Beanwrapperinterface and its corresponding implementation (Beanwrapperimpl). Javadocsaid,Beanwrappercan be set (Set) and get (Get) property values (either individually or in large quantities), you can get the property descriptor (Property Descriptors), you can also query whether the property is readable or writable. Beanwrapperalso supports nested properties, the ability to set properties of child properties and support for infinite extensions. In addition,Beanwrappersupport for adding standardsJavabensPropertychangelistenersand thevetoablechangelisteners,instead of needing to be in the target classprovideCode。 The last point is also important,Beanwrappersupport for setting indexed properties. Beanwrapperis not normally used directly by the application code, butDataBinderand thebeanfactorybut not in the middle.

Beanwrapper is like its name: wrapping a bean to provide additional operations, such as setting or retrieving property values.


Set and get basic and nested properties

Use both the setPropertyValue (s) and GetPropertyValue (s) methods to set and get properties. both of these methods contain a pair of overloaded methods. It is necessary to understand that there are some conventions on the properties of an object, such as:

Below you will find some examples of using beanwrapper to get and set properties

(If you're not going to work directly with Beanwrapper , the next section isn't very important to you.) If you just use databinder,beanfactory and they encapsulate the implementation, then you should jump to Propertyeditors this section)

Consider the following 2 classes

public class Company {private String name;private Employee managingdirector;public string GetName () {return this.name;} public void SetName (String name) {this.name = name;} Public Employee Getmanagingdirector () {return this.managingdirector;} public void Setmanagingdirector (Employee managingdirector) {this.managingdirector = Managingdirector;}}

public class Employee {private String name;private float salary;public string getName () {return this.name;} public void SetName (String name) {this.name = name;} public float getsalary () {return salary;} public void Setsalary (float salary) {this.salary = salary;}}

The following code snippet shows how to retrieve and manipulate companies employees properties

public static void Main (String args[]) {Beanwrapper company = new Beanwrapperimpl (new company);//Setting the company NA Me.. Company.setpropertyvalue ("name", "Some Company Inc."); /... can also be do like this:propertyvalue value = new PropertyValue ("name", "Some Company Inc."); Company.setpropertyvalue (value);//OK, let's create the director and tie it to the company:beanwrapper jim = new Beanwrapp Erimpl (New Employee ()); Jim.setpropertyvalue ("Name", "Jim Stravinsky"); Company.setpropertyvalue ("Managingdirector" , Jim.getwrappedinstance ());//Retrieving the salary of the managingdirector through the companyfloat salary = (Float) com Pany.getpropertyvalue ("Managingdirector.salary");}




6.4 Bean operation and one of the Beanwrapper classes

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.