(spring-12th back to "IOC Basics") JavaBean Property Editor

Source: Internet
Author: User

In the final phase of the spring instantiation Bean, Spring uses the property editor to convert the text configuration values in the configuration file to the corresponding values of the bean properties, for example:

Code 001
1<id= "Car" class= "Com.mesopotamia.test1.Car"2 p:name= "Car"3 p:brand= "BMW"4 P:maxspeed = "$" />

Above is the text configuration,

Look at the bean again:

Code 002
1publicclass Car {2 private String name; 3 Private String brand; 4 Private Double maxspeed; 5 ...

Above is the Bean's properties.

Then, the code 001 "car", "BMW", "200" is a simple value, and does not indicate the type, such as: String, double, and so on, this is the literal value, then the code 001 in the "literal" into the code 002 of the car class specific property values, is the property editor's work.

Before you introduce spring's property editor, it is important to understand the JavaBean property editor.

The JavaBean specification that Sun has developed is largely prepared for the IDE. (ide:integrated development environment, integrated development environment, typically includes code editors , compilers , debuggers, and graphical user interface tools ). The JavaBean property Editor is the IDE's right-hand helper.

Unlike spring's property editing, the IDE uses the property editor to visually edit the properties of a component.

In general, it takes three important parts to complete the JavaBean editing work:

    1. Bean class. Of course, property editing is to set the Bean's property value, so the host must be in.
    2. BeanInfo. Through it we can know which properties of the bean can be edited, and which is the property editor for each property of the bean. (as if the Lao Wang's family has four girls, like Qin, chess, books, paintings, then the old Wang wants to cultivate these four girls, the big girl sent to the Qin society, two girl sent to Chess Square, three girl sent to the college, four girl sent to the painting building. These four girls are the four attributes that need editing, and the Piano Club, Chess Square, college, and painting building are four different attribute editors. )
    3. PropertyEditor. It knows through the bean class what I want to edit is the property of the bean, and through BeanInfo knows which property I want to edit. It is responsible for editing (converting) this property.

Here's a concrete chestnut:

The bean classes are as follows:

1  Public class extends jpanel{2    privateint titleposition = CENTER;  3    Private Boolean inverse; 4    // Omit Get/setter Method 5 }

JPanel is a panel container class in the Java graphical user interface (GUI) Toolkit swing, which is contained in the Javax.swing package and is a lightweight container that can be added to the JFrame form. If a bean inherits JPanel, it means that we can edit the properties of the bean through a visual graphical interface.

BeanInfo as follows:

1 Importjava.beans.*;2   Public classChartbeanbeaninfoextendssimplebeaninfo{3      Publicpropertydescriptor[] getpropertydescriptors () {4        Try{5 6 //① binding Titlepositioneditor to Chartbean's Titleposition attribute7  propertydescriptor titlepositiondescriptor 8 = new Prop Ertydescriptor ("Titleposition", Chartbean.  Class); 9          Titlepositiondescriptor.setpropertyeditorclass (titlepositioneditor.  Class); Ten             One   A //② binding Inverseeditor to Chartbean's inverse attribute - PropertyDescriptor Inversedescriptor -=NewPropertyDescriptor ("Inverse", Chartbean.class);  theInversedescriptor.setpropertyeditorclass (Inverseeditor.class); -           return Newpropertydescriptor[]{titlepositiondescriptor, inversedescriptor}; -        } -        Catch(introspectionexception e) { + e.printstacktrace (); -           return NULL; +        } A     } at}

For the BeanInfo class above, here are a few things to know:

    1. BeanInfo naming rules in JavaBean: The Bean's class name +beaninfo, such as: Chartbean+beaninfo.
    2. Simplebeaninfo implements the BeanInfo class, which, in general, implements its own functionality by extending the class.
    3. A property descriptor (PropertyDescriptor) records a property and the Bean class to which the attribute belongs.
    4. In line 7th and 8, BeanInfo binds the attribute to the bean and plugs it into a PropertyDescriptor object.
    5. Immediately following line 9th, the property descriptor that binds the property and Bean class is connected to a specific property editor, such as this example: Titlepositioneditor.

12 rows followed by another property operation, similar to the one described above.

The following is the property editor class:

1 Importjava.beans.*;2   Public classTitlepositioneditorextendspropertyeditorsupport{3     Privatestring[] options = {"Left", "Center", "right" }; 4 5     //① An array of string identifiers that represent the value of an optional property6    Public   string[] GetTags () { return  options;}  7    8     //② string representing the initial value of the property9    Public   String getjavainitializationstring () { return "" + getValue ();} Ten      One     //③ converts internal property values to corresponding string representations for display by the property editor A  Public    String getastext () {     -        intValue =(Integer) getValue (); -        returnOptions[value]; the     } -  -     //④ converting an externally set string to the value of an internal property -   Public   void    Setastext (String s) {  +         for(inti = 0; i < options.length; i++){ -           if(Options[i].equals (s)) { + SetValue (i); A              return; at           } -        } -     } -}
PropertyEditorSupport defines some methods of attribute editing, typically by extending the class to define its own property editor. We can understand the above Titlepositioneditor method according to the result of the graphic interface below:

  1. Line six, GetTags () is the property editing interface and a property corresponding to the drop-down box selection, the code returned is an array of opertions strings, rendering to the operator interface such as the drop-down box: Left, Center, right, and so on.
  2. Line 9th,getjavainitializationstring () is the initial value of the set edit. GetValue () is the current value that gets the current property. The second line of code for the Bean class:private int titleposition = center, set the initial value of Titleposition to center (note that this is a constant), then GetValue () Returns the encapsulated class instance that corresponds to the center value. (the base type is encapsulated as an object to store). and getjavainitializationstring () gets this initial value, so we see the default value for the graphical interface drop-down box is center. Just like the drop-down box for HTML:
    1 <Select>2   <optionvalue= "0">Left</option>3   <optionvalue= "1"selected>Center</option>4   <optionvalue= "2">Right</option>5 </Select>

  3. Line 12th, for example, like the above HTML, the value of the property is actually "1", but, "1" represents the Center, so Getastext () is based on the attribute value "1" into the corresponding string "Center", so that, The drop-down box on the interface shows the string center instead of "1". Getastext () is to help display the graphical interface.
  4. Line 18th, when we use the dropdown box to select right, then we need to convert to the corresponding property value (for example, 2), so as to assign a value to the Bean's Titleposition property, Setastext is for this conversion.
  5. Combining 12, 18 rows We know that this is actually a two-way process, the editor gets the optional value of the property and the current value, converted to the corresponding string into the drop-down box for the user to choose, after the selection, the editor will select the string value to the corresponding property value, assign to the bean properties. GetValue () is responsible for getting the value of the current property. This process is the working mechanism of the property editor.

The Spring property editor does not have a UI interface, it is only responsible for converting the literal value in the configuration file into the Bean's property values, so the working mechanism is not equivalent to the Java Bean's property editor. But to understand the Java Bean's property editor, and then to understand the Spring property editor working mechanism, that is minutes to get things done. If you anticipate your funeral, listen to tell.

    

       Hundred studies must be determined first. --Zhu Hei

(spring-12th back to "IOC Basics") JavaBean Property Editor

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.