Sie sind auf Seite 1von 4

Java Beans

Introduction to JavaBeans:

The specification by Sun Microsystems defines JavaBeans as “reusable software


components that can be manipulated visually in a builder tool”.
The JavaBeans architecture is based on a component model which enables developers
to create software units called components. Components are self-contained, reusable software
units that can be visually assembled into composite components, applets, applications and
servlets using visual application builder tool. JavaBeans components are known as beans.
Every Java bean component should follow JavaBeans specification, which define the way by
which different components can communicate with each other, interact with a builder tool,
save/restore their configuration etc. In addition to this it also defines the architecture of a single
class in the software component.

A JavaBean is a simple java class, adhering to certain conventions about method


naming constructors and behaviors. The required conventions for JavaBeans are:
 I t should have a no-argument constructor so that its object can be created without
knowing any thing about bean.
 Its properties must be accessible using getter/setter and other methods (collectively
known as accessor/methods) following a standard naming convention. As the name
implies, getter method is used to obtain the value of a property, while setter method is
used to set the value of property.

Advantages of Java Beans:

A software component architecture provides standards mechanism to deal with


software building blocks. The following list enumerated some of the specific benefits that java
technology provides for a component developer.
 Instead of reusing Java Classes (Write-Once-Run-Anywhere WORA) a bigger size of
code (component) is reused (Write-Once-Deploy-Anywhere WODA). Thereby taking
code reuse to a while new level.
 The properties, events, and methods of a Bean that are exposed to an application
builder tool can be controlled.
 The configuration settings of a bean can be saved in persistent storage and restored at
a later time.
 A Bean may register to receive events from other Objects and can generate events that
are sent to other objects, thereby making it useful for Interactive applications.
 Bean can be configured by using auxiliary software of users, choice.
Create and Configure an Instance of the OurButton Bean:
Follow these steps to create and configure an instance of the OurButton Bean and
connect it to the Molecule Bean:
 Position the cursor on the ToolBox entry labeled OurButton and click the left mouse
button. You should see the cursor change to a cross.
 Move the cursor to the BeanBox display area and click the left mouse button in
approximately the area where you wish the Bean to be displayed. You should see a
rectangular region appear that contains a button. This area is surrounded
 by a hatched border indicating that it is currently selected.
 You may reposition the OurButton Bean by positioning the cursor over one of the
hatched borders and dragging the Bean.
 Go to the Properties window and change the label of the Bean to “Rotate X”. The
button appearance changes immediately when this property is changed.
 Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed.
You should now see a line extending from the button to the cursor. Notice that one end
of the line moves as the cursor moves. However, the other end of the line remains fixed
at the button.
 Move the cursor so that it is inside the Molecule Bean display area, and click the left
mouse button. You should see the Event Target Dialog dialog box.
 The dialog box allows you to choose a method that should be invoked when this button
is clicked. Select the entry labeled “rotateOnX” and click the OK button. You should see
a message box appear very briefly, stating that the tool is “Generating and compiling
adaptor class.”
 Test the application. Each time you press the button, the molecule should move a few
degrees around one of its axes.

Now create another instance of the OurButton Bean. Label it “Rotate Y” and map its
action event to the “rotateY” method of the Molecule Bean. The steps to do this are very
similar to those just described for the button labeled “Rotate X”.Test the application by clicking
these buttons and observing how the molecule moves.

Introspection:
Introspection is the automatic process of analyzing a bean’s design patterns to reveal
the bean’s properties, events and methods. Introspection asks JavaBeans components what are
the properties and events it supports. The JavaBeans API provides a great deal of Support for
introspection so as to enable the application builder tool to find out information about that
structures and functionality of a bean , we cannot think of JavaBeans technology without
considering introspection mechanism. There are two ways of support introspection. In the first
method of introspection is supported by reflection where you name methods with certain
conventions, like set/getProperty() and add/Listener(). In the second method you explicitly
expose the bean’s behavior through a class which extends SimpleBeanInfo which in turn
provides default implementation of BeanInfo interface.
Developing a Simple Bean using the BDK:
Here are the steps that you must follow to create a new Bean:

o Create a directory for the new Bean.


c:\bdk\demo\sunw\demo\colors.
o Create the Java source file(s).
o Compile the source file(s).
javac Colors.java.
o Create a manifest file.
Name: sunw/demo/colors/Colors.class
Java-Bean: True
In the location :c:\bdk\demo\colors.mft.
o Generate a JAR file.
jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class
o Start the BDK.
o Test.

Create and Configure an Instance of the OurButton Bean


Create an instance of the OurButton Bean in the BeanBox window. Then follow
these steps:
1. Go to the Properties window and change the label of the Bean to “Change”. You
should see that the button appearance changes immediately when this property is
changed.
2. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed.
3. Move the cursor so that it is inside the Colors Bean display area, and click the left mouse
button. You should see the Event Target Dialog dialog box.
4. The dialog box allows you to choose a method that should be invoked when this button
is clicked. Select the entry labeled “change” and click the OK button. You should see a
message box appear very briefly, stating that the tool is “Generating and compiling
adaptor class.”
5. Click on the button. You should see the color change.

Bound Properties:

Sometimes when a Bean property changes, another object might need to be notified of
the change and react to the change, we can do this by making a property bound property.
Therefore when a bound property changes, notification of the change is sent to the interested
listeners through java.beans.PropertyChangeEvent class.
Bound properties support java.beans.PropertyChangeListener so as to receive
PropertyChangeEvent notifications. The accessor methods for a bound property are defined in
the same way as those for simple properties. However you also provide the event listener
registration methods for PropertyChangeListener classes and fire a PropertyChangeEvent
event to the PropertyChangeListener object by calling their PropertyChange methods. The
convenience java.beans.PropertyChangeSupport class enables your bean to implement these
methods. In order to listen the property change, an object must be able to add and remove
itself from listener list in the bean containing the bound property. It must also be able to
respond to the event notification method that signals a property change.
Create an instance of the TickTock Bean. The Properties window should show one
property for this component. It is “Interval” and its initial value is 5. This represents the number
of seconds that elapse between property change events generated by the TickTock Bean.
Change the value to 1.
Now you need to map events generated by the TickTock Bean into method calls on
the Colors Bean. Follow these steps:
 Go to the menu bar of the BeanBox and select Edit | Events | propertyChange |
propertyChange. You should now see a line extending from the button to the cursor.
 Move the cursor so that it is inside the Colors Bean display area, and click the left mouse
button. You should see the Event Target Dialog dialog box.
 The dialog box allows you to choose a method that should be invoked when this event
occurs. Select the entry labeled “change” and click the OK button. You should see a
message box appear very briefly, stating that the tool is “Generating and compiling
adaptor class.”
You should now see the color of your component change every second.

Constrained Properties:

Constrained properties are more complicated that bound properties because they also
support property change listener which happen to be vetoers. i.e., the listener may accept the
property change or forbid it.
A bean property is constrained it the bean supports the
java.beans.VetoableChangeListener and java.bean.PropertyChangeEvent classes and if the set
method for this property throws a java.beans.PropertyVetoException.
The accessor methods for a constrained property are defined in the same way as those
for simple properties, with the addition that the setXXX method throws a
PropertyVetoException.
public void setPropertyName(PropertyType value)throws PropertyVetoException
{. . .}

Persistence:
Persistence means storing the data in a durable space so that is can be accessed at a
later time by the same application or another. In persistence data outlives the process that
created it. Remember if you want a class to be persisted then it should implement
java.io.Serializable.
In the context of JavaBeans we configure Beans with all its necessary configuration
setting, so that it can be later restored with same configuration setting.

Customizes:

The Properties window of the BDK allows a developer to modify the properties of a
Bean. However, this may not be the best user interface for a complex component with many
interrelated properties. Therefore, a Bean developer can provide a customizer that helps
another developer configure this software. A customizer can provide a step-by-step guide
through the process that must be followed to use the component in a specific context. Online
documentation can also be provided. A Bean developer has great flexibility to develop a
customizer that can differentiate his or her product in the marketplace.

Das könnte Ihnen auch gefallen