JavaBeans Wiki Selected Passage

Source: Internet
Author: User
Tags configuration settings

Given that spring's beans package complies with JavaBean specs, it is necessary to study JavaBean specs carefully.

Let's take a look at what the wiki says:

Defined

Under the Java platform, JavaBeans is the class that contains a lot of objects into a single object, and this single object is the bean. JavaBeans features: Serializable, non-parametric structure, setter/getter properties.

Advantages:

    • The properties, events, and methods of the bean are controllable.
    • Beans can be registered to receive events from other objects, and events can be generated and sent to other objects.
    • can provide auxiliary software to configure a bean.
    • The configuration settings of a bean can be persisted and restored.

Disadvantages:

    • The state of a non-parametric constructed class is invalid when instantiated. Developer may not be aware of abnormal instantiation.
    • JavaBeans are modifiable, so they do not have the advantage of non-modifiable objects.
    • Many setter/getter must be created, resulting in a lot of stiff code.

JavaBeans API

The functionality of JavaBean is provided by the classes and interfaces in the Java.beans package.

Interface Describe
Appletinitializer Used to initialize beans that are applets at the same time.
BeanInfo Allows the designer to describe in detail information about the events, methods, and properties of a bean.
Customizer Allows the designer to provide a GUI that can configure a bean.
DesignMode Determines whether a bean is executing under design mode.
Exceptionlistener When an exception occurs, a method of the interface is called.
PropertyChangeListener When a bound property is changed, a method of that interface is called.
PropertyEditor The implementation of this interface allows the designer to modify and display the property values.
Vetoablechangelistener When a constrainted property is changed, a method of that interface is called.
Visibility The methods in this interface allow the bean to execute in an environment where the GUI is not available.

Note: Bound property and Constrainted property, see Properties.

JavaBeans Conventions (Convention)

    • The class must have a public parameterless construct.
    • The properties of the class must be accessible through get/set. -boolean properties can be accessed through the IS.
    • The class must be serializable.

code example:

1  Packageplayer;2 3  Public classPersonbeanImplementsjava.io.Serializable {4 5     /**6 * Property <code>name</code> (note capitalization) readable/writable.7      */8     PrivateString name =NULL;9 Ten     Private Booleandeceased =false; One  A     /**No-arg Constructor (takes No arguments).*/ -      PublicPersonbean () { -     } the  -     /** - * Getter for property <code>name</code> -      */ +      PublicString GetName () { -         returnname; +     } A  at     /** - * Setter for property <code>name</code>. -      * @paramvalue -      */ -      Public voidSetName (FinalString value) { -Name =value; in     } -  to     /** + * Getter for property "deceased" - * Different syntax for a Boolean field (is vs. get) the      */ *      Public Booleanisdeceased () { $         returndeceased;Panax Notoginseng     } -  the     /** + * Setter for property <code>deceased</code>. A      * @paramvalue the      */ +      Public voidSetdeceased (Final Booleanvalue) { -deceased =value; $     } $}
View Code

Simple test:

1 Importplayer. Personbean;2 3 /**4 * Class <code>testpersonbean</code>.5  */6  Public classTestpersonbean {7     /**8 * Tester method <code>main</code> for Class <code>personbean</code>.9      * @paramARGSTen      */ One      Public Static voidMain (string[] args) { APersonbean person =NewPersonbean (); -  -Person.setname ("Bob"); thePerson.setdeceased (false); -  -         //Output: "Bob [Alive]" - System.out.print (Person.getname ()); +System.out.println (person.isdeceased ()? "[Deceased]": "[Alive]"); -     } +}
View Code

Use in JSP:

1 <% //Use of the Personbean in a JSP.%>2 <Jsp:usebeanID= "Person"class= "Player." Personbean "Scope= "page"/>3 <Jsp:setpropertyname= "Person" Property="*"/>4 5 <HTML>6     <Body>7Name:<Jsp:getpropertyname= "Person" Property= "Name"/><BR/>8Deceased?<Jsp:getpropertyname= "Person" Property= "Deceased"/><BR/>9         <BR/>Ten         <formname= "Beantest"Method= "POST"Action= "testpersonbean.jsp"> OneEnter a name:<inputtype= "text"name= "Name"size= " the"><BR/> A Choose an option: -             <Selectname= "Deceased"> -                 <optionvalue= "false">Alive</option> the                 <optionvalue= "true">Dead</option> -             </Select> -             <inputtype= "Submit"value= "Test the Bean"> -         </form> +     </Body> - </HTML>
View Code

Reference Links:

    • Oracle ' s JavaBeans tutorials
    • JavaBeans specification

JavaBeans Wiki Selected Passage

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.