JavaBeans

Last updated

In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

Contents

The 'beans' of JavaBeans are classes that encapsulate one or more objects into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier code reuse and introspection. This in turn allows the beans to be treated as software components, and to be manipulated visually by editors and IDEs without needing any initial configuration, or to know any internal implementation details.

As part of the standardization, all beans must be serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.

Features

Introspection
Introspection is a process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans specification because it allows another application, such as a design tool, to obtain information about a component.
Properties
A property is a subset of a Bean's state. The values assigned to the properties determine the behaviour and appearance of that component. They are set through a setter method and can be obtained by a getter method.
Customization
A customizer can provide a step-by-step guide that the process must follow to use the component in a specific context.
Events
Beans may interact with the EventObject EventListener model.[ clarification needed ]
Persistence
Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
Methods
A Bean should use accessor methods to encapsulate the properties. A Bean can provide other methods for business logic not related to the access to the properties.

Advantages

Disadvantages

JavaBeans API

The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.

InterfaceDescription
AppletInitializerMethods in this interface are used to initialize Beans that are also applets.
BeanInfoThis interface allows the designer to specify information about the events, methods and properties of a Bean.
CustomizerThis interface allows the designer to provide a graphical user interface through which a bean may be configured.
DesignModeMethods in this interface determine if a bean is executing in design mode.
ExceptionListenerA method in this interface is invoked when an exception has occurred.
PropertyChangeListenerA method in this interface is invoked when a bound property is changed.
PropertyEditorObjects that implement this interface allow the designer to change and display property values.
VetoableChangeListenerA method in this interface is invoked when a Constrained property is changed.
VisibilityMethods in this interface allow a bean to execute in environments where the GUI is not available.

JavaBean conventions

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.

The required conventions are as follows:

Code example

packageorg.wikipedia.players;importjava.io.Serializable;importjava.util.List;publicclassPersonBeanimplementsSerializable{/** Properties **/privatebooleandeceased=false;privateList<String>list;/** Property "name", readable/writable. */privateStringname=null;/** No-arg constructor (takes no arguments). */publicPersonBean(){}publicList<String>getList(){returnlist;}publicvoidsetList(finalList<String>list){this.list=list;}/**     * Getter for property "name".     */publicStringgetName(){returnname;}/**     * Setter for property "name".     *     * @param value     */publicvoidsetName(finalStringvalue){this.name=value;}/**     * Getter for property "deceased"     * Different syntax for a boolean field (is vs get)     */publicbooleanisDeceased(){returndeceased;}/**     * Setter for property "deceased".     * @param value     */publicvoidsetDeceased(booleanvalue){deceased=value;}}

TestPersonBean.java:

packageorg.wikipedia.players;importjava.util.ArrayList;importorg.wikipedia.players.PersonBean;/** * Class "TestPersonBean". */publicclassTestPersonBean{/**     * Tester method "main" for class "PersonBean".     *     * @param arguments     */publicstaticvoidmain(String[]args){finalPersonBeanperson=newPersonBean();person.setName("Bob");person.setDeceased(false);person.setList(newArrayList<String>());// Output: "Bob is [alive]"System.out.printf("%s is %s%n",person.getName(),person.isDeceased()?" [deceased]":" [alive]");}}
<jsp:useBeanid="person"class="org.wikipedia.players.PersonBean"scope="page"/><jsp:setPropertyname="person"property="*"/><html><body>Name:<jsp:getPropertyname="person"property="name"/><br/>Deceased?<jsp:getPropertyname="person"property="deceased"/><br/><br/><formname="beanTest"method="POST"action="testPersonBean.jsp">Enteraname:<inputtype="text"name="name"size="50"><br/>Chooseanoption: <selectname="deceased"><optionvalue="false">Alive</option><optionvalue="true">Dead</option></select><inputtype="submit"value="Test the Bean"></form></body></html>

See also

References

  1. 1 2 Bloch, Joshua (2008). Effective Java (Second ed.). Addison-Wesley. p.  13. ISBN   978-0-321-35668-0.