Java Reference
In-Depth Information
JavaBeans
Packages
java.beans, java.beans.beancontext
Use: J2SE (since JDK1.1)
Overview
JavaBeans™ provides a standardized model for the Java programming language that enables classes to be
developed as components. Components have a standard way of representing data and behavior, so they're more
easily shared between developers. A component model such as JavaBeans potentially enables a developer to reuse
another developer's code even if they work in different companies in different parts of the world. In the
component model, technical roles are divided into component programmers, component assemblers, and
application assemblers. The programmers are the only ones who actually have to code; the component and
application assemblers use development tools that allowed them to visually manipulate and combine beans. This
enables them to build new beans, or entire applications. JavaBeans can be visual, but they don't have to be.
That a component model is a good idea is demonstrated by the fact that components also underlie Java 2
Enterprise Edition, but the visual use of JavaBeans was only moderately successful.
However, that isn't to say that JavaBeans are insignificant. When JavaBeans were conceived they required quite a
change in Java. The old event model (hierarchical) had to be replaced with a more flexible event model so
responsibilities could properly be distributed. The new event model became known as the delegation model. (See
Event Handling ” on page 281.) This event model is core to the Java Beans architecture.
At the same time, emphasis was also placed on code conventions, because naming is an essential part of the introspection of JavaBeans.
All of the AWT components became JavaBeans.
JavaBeans are generally supposed to support events, properties, introspection, customization, and persistence.
The current event model allows decoupling event sources and event listeners. Every JavaBean can be a source of
events and/or a listener to events. The JavaBean identifies itself as a listener by implementing the appropriate
listener interfaces and methods defined in those interfaces. To identify itself as an event source, the bean has to
provide addXXXListener and remove XXX Listener methods.
For other tools and applications to find the properties of a JavaBean, the bean has to stick to specific method
naming. If the bean has what we now know as getters and setters with a particular name, like String getName()
and void setName(String n) , then other applications can safely assume the bean has a property called name of a
type String , even though the internal representation may be different. This property can then be used in property
sheets of a visual editor or in other applications. JSPs, JavaServer Pages, make use of JavaBeans in this way.
Values returned from an HTML form are set as properties, which can later be retrieved for some processing.
Normally, all public methods are exported. If bean providers want to limit the number of properties, events, and
methods exported, they can supply a class that implements the BeanInfo interface. The BeanInfo interface
defines methods for other objects to easily query what members and events are available. The code that
determines which methods are exported is in the implementation of BeanInfo .
A bean can provide its own PropertyEditor for new data types, allowing the bean to be included in a visual
component environment. Such an editor can either support Strings as values, or it may even use its own
java.awt.Component to do the editing.
When the bean can provide its own customizer for more complex customization, that type of editor should extend
from java.awt.Component and implement java.beans.Customizer so that a graphical editor can integrate the
editor in the GUI.
A JavaBean needs to support some way of persisting itself, so it has to either implement java.io.Serializable
or java.io.Externalizable . When the bean is persisted, its internal state should be saved so that the bean may
later be restored with the same data. This serialized version of a bean can even be treated as its own type. The
java.beans.Beans.instantiate method takes several arguments, one of which is the name of a Bean as a
String . The instantiate method first tries to locate a file with the specified name with a trailing .ser ; if that fails,
it tries to locate a class with the bean name and, if found, instantiate that.
 
Search WWH ::




Custom Search