Java Bean

*

*
Chapter 6: JavaBeans
*

6.2.

Bạn đang xem: Java bean

JavaBeans Conventions

The JavaBeans component Mã Sản Phẩm relies on a number of rules và conventions bean developers must follow. These conventions are not part of the JavaBeans API itself, but in many ways, they are more important khổng lồ bean developers than the API itself. The conventions are sometimes referred khổng lồ as designpatterns ; they specify such things as method names & signatures for property accessor methods defined by a bean.

The reason for these kiến thiết patterns is interoperability between beans & the beanbox programs that manipulate them. As we"ve sầu seen, beanbox programs may rely on introspection to determine the các mục of properties, events, and methods a bean supports. In order for this khổng lồ work, bean developers must use method names the beanbox can recognize. The JavaBeans framework facilitates this process by establishing naming conventions. One such convention, for example, is that the getter and setter accessor methods for a property should begin with get và set.

Not all the patterns are absolute requirements. If a bean has property accessor methods that bởi not follow the naming conventions, it is possible khổng lồ use a PropertyDescriptor object (specified in a BeanInfo class) to lớn indicate the accessor methods for the property. Although the BeanInfo class provides an alternative to lớn the property-accessor-method naming convention, the property accessor method must still follow the conventions that specify the number và type of its parameters and its return value.

6.2.1. Beans

A bean itself must adhere to lớn the following conventions:

Class name

There are no restrictions on the class name of a bean.

Superclass

A bean can extend any other class. Beans are often AWT or Swing components, but there are no restrictions.

Instantiation

A bean must provide a no-parameter constructor or a file that contains a serialized instance the beanbox can deserialize for use as a prototype bean, so a beanbox can instantiate the bean. The tệp tin that contains the bean should have the same name as the bean, with an extension of .ser.

Bean name

The name of a bean is the name of the class that implements it or the name of the file that holds the serialized instance of the bean (with the .ser extension removed và directory separator (/) characters converted lớn dot (.) characters).

6.2.2. Properties

A bean defines a property p of typeT if it has accessor methods thatfollow these patterns (ifT is boolean, a special khung of getter method is allowed):

Getter

public T getP()Boolean getterpublic boolean isP()Setterpublic void setP(T)Exceptions Property accessor methods can throw any type of checked or unchecked exceptions

6.2.3. Indexed Properties

An indexed property is a property of arraytype that provides accessor methods that get & set the entirearray, as well as methods that get và set individual elements ofthe array. A bean defines an indexed propertyp of typeT<> if it definesthe following accessor methods:

Array getter

public T<> getP()Element getterpublic T getP(int)Array setterpublic void setP(T<>)Element setterpublic void setP(int,T)Exceptions Indexed property accessor methods can throw any type of checked or unchecked exceptions. In particular, they should throw an ArrayIndexOutOfBoundsException if the supplied index is out of bounds.

6.2.4. Bound Properties

A bound property is one that generates aPropertyChangeEvent when its value changes. Here are the conventions for a bound property:

Accessor methods

The getter and setter methods for a bound property follow the same conventions as a regular property.

Introspection

A beanbox cannot distinguish a bound property from a nonbound property through introspection alone. Therefore, you may want to implement a BeanInfo class that returns a PropertyDescriptor object for the property. The isBound() method of this PropertyDescriptor should return true.

Listener registration

A bean that defines one or more bound properties must define a pair of methods for the registration of listeners that are notified when any bound property value change. The methods must have sầu these signatures:

public void addPropertyChangeListener(PropertyChangeListener)public void removePropertyChangeListener(PropertyChangeListener)

Named property listener registration

A bean can optionally provide additional methods that allow event listeners lớn be registered for changes khổng lồ a single bound property value. These methods are passed the name of a property và have the following signatures:

public void addPropertyChangeListener(String, PropertyChangeListener)public void removePropertyChangeListener(String, PropertyChangeListener)

Per-property listener registration

A bean can optionally provide additional event listener registration methods that are specific to a single property. For a property p, these methods have the following signatures:

public void addPListener(PropertyChangeListener)public void removePListener(PropertyChangeListener) Methods of this type allow a beanbox lớn distinguish a bound property from a nonbound property.

Notification

When the value of a bound property changes, the bean should update its internal state lớn reflect the change & then pass a PropertyChangeEvent khổng lồ the propertyChange() method of every PropertyChangeListener object registered for the bean or the specific bound property.

Support

java.beans.PropertyChangeSupport is a helpful class for implementing bound properties.

6.2.5. Constrained Properties

A constrained property is one for which any changes can bevetoed by registered listeners. Most constrained properties arealso bound properties. Here are the conventions for aconstrained property:

Getter

The getter method for a constrained property is the same as the getter method for a regular property.

Setter

The setter method of a constrained property throws a PropertyVetoException if the property change is vetoed. For a property p of type T, the signature looks lượt thích this:

public void setP(T) throws PropertyVetoException

Listener registration

A bean that defines one or more constrained properties must define a pair of methods for the registration of listeners that are notified when any constrained property value changes. The methods must have sầu these signatures:

public void addVetoableChangeListener(VetoableChangeListener)public void removeVetoableChangeListener(VetoableChangeListener)

Named property listener registration

A bean can optionally provide additional methods that allow event listeners lớn be registered for changes lớn a single constrained property value.

Xem thêm: Don'T Mess Around Là Gì ? What Is The Meaning Of Mess Around

These methods are passed the name of a property & have the following signatures:

public void addVetoableChangeListener(String, VetoableChangeListener)public void removeVetoableChangeListener(String, VetoableChangeListener)

Per-property listener registration

A bean can optionally provide additional listener registration methods that are specific khổng lồ a single constrained property. For a property p, these methods have sầu the following signatures:

public void addPListener(VetoableChangeListener)public void removePListener(VetoableChangeListener)

Notification

When the setter method of a constrained property is invoked, the bean must generate a PropertyChangeEvent that describes the requested change & pass that sự kiện lớn the vetoableChange() method of every VetoableChangeListener object registered for the bean or the specific constrained property. If any listener vetos the change by throwing a PropertyVetoException, the bean must sover out another PropertyChangeEvent khổng lồ revert the property to its original value, and then it should throw a PropertyVetoException itself. If, on the other hvà, the property change is not vetoed, the bean should update its internal state to lớn reflect the change. If the constrained property is also a bound property, the bean should notify PropertyChangeListener objects at this point.

Support

java.beans.VetoableChangeSupport is a helpful class for implementing constrained properties.

6.2.6. Events

In addition khổng lồ PropertyChangeEvent eventsgenerated when bound và constrained properties are changed, abean can generate other types of events. An sự kiện namedE should follow these conventions:

Event class

The sự kiện class should directly or indirectly extkết thúc java.util.EventObject & should be named EEvent.

Listener interface

The sự kiện must be associated with an event listener interface that extends java.util.EventListener and is named EListener.

Listener methods

The event listener interface can define any number of methods that take a single argument of type EEvent and return void.

Listener registration

The bean must define a pair of methods for registering sự kiện listeners that want to be notified when an E event occurs. The methods should have the following signatures:

public void addEListener(EListener)public void removeEListener(EListener)

Unicast events

A unicast sự kiện allows only one listener object to lớn be registered at a single time. If E is a unicast sự kiện, the listener registration method should have this signature:

public void addEListener(EListener) throws TooManyListenersException

6.2.7. Methods

A beanbox can expose the methods of a bean to lớn applicationdesigners. The only formal convention is that these methodsmust be declared public. The followingguidelines are also useful, however:

Method name

A method can have sầu any name that does not conflict with the property- and event-naming conventions. The name should be as descriptive sầu as possible.

Parameters

A method can have sầu any number và type of parameters. However, beanbox programs may work best with no-parameter methods or methods that have sầu simple primitive sầu parameters.

Excluding methods

A bean can explicitly specify the menu of methods it exports by providing a BeanInfo implementation.

Documentation

A bean can provide user-friendly, human-readable localized names & descriptions for methods through MethodDescriptor objects returned by a BeanInfo implementation.

6.2.8. Auxiliary Classes

A bean can provide the following auxiliary classes:

BeanInfo

To provide additional information about a bean B, implement the BeanInfo interface in a class named BBeanInfo.

Property editor for a specific type

To enable a beanbox khổng lồ work with properties of type T, implement the PropertyEditor interface in a class named TEditor. The class must have sầu a no-parameter constructor.

Property editor for a specific property

To customize the way a beanbox allows the user to enter values for a single property, define a class that implements the PropertyEditor interface & has a no-parameter constructor, & register that class with a PropertyDescriptor object returned by the BeanInfo class for the bean.

Customizers

To define a customizer, or wizard, for configuring a bean B, define an AWT or Swing component with a no-parameter constructor that does the customization. The class is commonly called BCustomizer, but this is not required. Register the class with the BeanDescriptor object returned by the BeanInfo class for the bean.

Documentation

Define default documentation for a bean B in HTML 2.0 format and store that documentation in a tệp tin named B.html. Define localized translations of the documentation in files by the same name in locale-specific directories.

6.2.9. Bean Packaging & Distribution

Beans are distributed in JAR archive sầu files that have the following:

Content

The class or classes that implement a bean should be included in the JAR file, along with auxiliary classes such as BeanInfo và PropertyEditor implementations. If the bean is instantiated from a serialized instance, that instance should be included in the JAR archive sầu with a filename ending in .ser. The JAR tệp tin can contain HTML documentation for the bean và should also contain any resource files, such as images, required by the bean & its auxiliary classes. A single JAR tệp tin can contain more than one bean.

Java-Bean attribute

The manifest of the JAR file must mark any .class & .ser files that define a bean with the attribute:

Java-Bean: true

Depends-On attribute

The manifest of a JAR tệp tin can use the Depends-On attribute to lớn specify all other files in the JAR archive on which the bean depends. A beanbox application can use this information when generating applications or repackaging beans. Each bean can have zero or more Depends-On attributes, each of which can các mục zero or more space-separated filenames. Within a JAR file, / is always used as the directory separator.

Design-Time-Only attribute

The manifest of a JAR tệp tin can optionally use the Design-Time-Only attribute lớn specify auxiliary files, such as BeanInfo implementations, that are used by a beanbox, but not used by applications that use the bean. The beanbox can use this information when repackaging beans for use in an application.