Introduction
JavaBeans define a simple and powerful component model for Java. The goal of JavaBeans is to provide for self-contained
and reusable units that can be manipulated programmatically by developers or visually in builder tools.
JavaBeans can be GUI controls or they may be lacking visual representation. GUI controls in Java are typically
JavaBeans in order to be manipulated by the builder tools. In J2EE, simple JavaBeans are commonly used from JSPs, where
they provide for separation of presentation in HTML and the Java code, which is contained in the JavaBeans.
A JavaBean is a Java class with three distinctive features:
JavaBean Properties
Properties of a JavaBean can be access by other components. Typically, a property is a private value accessed through
setter and getter methods, but it can be a computed value as well. Updates of properties can have various side effects.
Accessors are methods for accessing properties. Accessors can be getters and setters, and they follow naming
conventions:
void setPropertyName(PropertyType value); // setter
PropertyType getPropertyName() // getter
For a boolean property, a getter can be:
boolean isPropertyName() // getter for a boolean property
Example: Customer Bean
The following is an example of a simple JavaBean (Customer) with two simple properties: name and email. Notice how the
properties are defined through the pairs of set/get methods.
public class Customer {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getEmail() {
return email;
}
public void setEmail(String aEmail) {
email = aEmail;
}
}
Such simple JavaBeans are often used in JSPs, where they enable transport of values from forms on the web pages.
Indexed Properties
Besides simple properties, which have a single value, a property can be a set of values contained in an array.
Indexed properties can return a value at the specified index, or the whole array of values.
Indexed properties have the following signatures:
void setPropertyName(int index, PropertyType value); // indexed setter
PropertyType getter(int index); // indexed getter
void setPropertyName(PropertyType values[]); // array setter
PropertyType[]getPropertyName(); // array getter
Bound Properties
The bound property mechanism provides for notification service when the property changes. Interested objects that wish
to be notified of the change register themselves in advance, and when the property change occurs, the registered
objects are sent the notification. Typically, this notification is done through an event fired by the component with the bound property after the property has been
set.
A JavaBean with a bound property exposes methods for registration and deregistration of interested objects, called
listeners. The developers can define their own notification mechanism, but the Java libraries provide a number of
commonly used support classes in the java.beans package.
Constrained Properties
Constrained properties are similar to bound properties, but the notification happens before the property is actually
set. That enables interested parties to disallow the change of the property by throwing a PropertyVetoException.
Events and Notification
Events are the feature of JavaBeans which enables independently developed components to communicate with each other by
propagating information about the state change. In this model, some components fire events, which are handled by other
components, in the role of the event listeners.
To support this communication model, JavaBeans component model provides:
-
properties that can fire events (bound and constrained properties)
-
registration methods, so that listeners can register themselves
-
events, which carry information about the change
-
listeners, which can react on the delivered event
The following class diagram illustrates these concepts for a JavaBean called EventSource with a simple
int property.
During configuration, concrete listeners register with the JavaBean. At some point later, some other object calls the
setProperty method, which will start the notification process by creating the event object. The EventSource
JavaBean will invoke the propertyChange method on all registered listeners. Event listeners will receive the
event, read the values from it and react on the event.
The following sequence diagram illustrates the order of invocations:
Notification of listeners is synchronous with respect to the JavaBean instance, which is the source of the event, but
the event listener can do the processing of events in another thread.
Introspection
Introspection is a run-time mechanism that enables detection of properties, events and methods of a JavaBean. Introspection is used by development tools and by
programs that do not use hard-coded dependencies on other components. Introspection is achieved through reflection and
a set of conventions for naming of methods and interfaces. Additional features of introspection are supported by the
BeanInfo class. JavaBeans naming conventions used for introspection are sometimes called "design patterns", but
they should not be confused with the notion of design pattern in object-oriented design.
Persistence
A JavaBean can be persisted through a serialization mechanism. The serialization can be automatic or custom, depending
if the JavaBean implements Serializable or Externalizable interfaces. JDK 1.4 introduces
XMLEncoder and XMLDecoder classes that provide for storing of JavaBeans instances in the XML format.
Customization
The appearance and behavior of a JavaBean can be customized at design time. This is particularly important for visual
JavaBeans that are used in graphical user interfaces. The customization is done through a property editor or by using
customizers. Customizers provide a custom user interface for configuring a JavaBean instance at design time.
BeanContext
BeanContext defines a logical containment hierarchy that enables JavaBeans to interrogate their environment for
capabilities and services. BeanContext mechanisms provide support for logical containment hierarchy of JavaBeans and
lookup of services offered by JavaBeans in the hierarchy.
JavaBeans™ Activation Framework
The Java Activation Framework is a standard Java extension which enables determination of the type of a piece of data,
encapsulation of it, discovery of its available operations and instantiation of a software component that corresponds
to the desired operation on the piece of data.
More Information
For more information on JavaBeans, see the JavaBeans API Specification, Version 1.01 at http://java.sun.com/. Follow the links to Docs & Training > Java 2 Platform, Standard
Edition > Java 2 SDK, SE v1.3 documentation > JavaBeans > JavaBeans Specification.
|