You are on page 1of 7

www.jntuworld.

com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-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 Java Bean 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:
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:

B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4


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 beans design patterns to
reveal the beans 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 beans behavior through a
class which extends SimpleBeanInfo which in turn provides default implementation of
BeanInfo interface.

B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4


Bean Properties:
Design pattern for properties
Property is a subset of a beans state. The values that are assigned to the
properties determine the behavior and appearance of that component.
Bean properties can be classified into
1. Simple properties
2. Indexed Properties
3. Bound Properties
4. Constrained properties
Simple properties:
A simple property has a single value. It can be identified by the following design patterns,
where N is the name of the property and T is its type.
Public T getN( );
Public void setN( );
Boolean properties:
A Boolean property has a value of true or false. It can be identified by the following
design patterns, where N is name of the property.
Public Boolean isN ( );
Public Boolean getN( );
Public void setN(Boolean value);
Indexed properties:
An indexed property consists of multiple values. It can be identified by the following
design patterns, where N is the name of the property and T is its type.
Public T getN(int index);
Public void setN(int index, T value);
Public T[ ] getN( );
Public void setN(T values[ ]);
Developing a Simple Bean using the BDK:
Here are the steps that you must follow to create a new Bean:
Create a directory for the new Bean.
c:\bdk\demo\sunw\demo\colors.
Create the Java source file(s).
Compile the source file(s).
javac Colors.java.
Create a manifest file.
Name: sunw/demo/colors/Colors.class
Java-Bean: True
In the location :c:\bdk\demo\colors.mft.
Generate a JAR file.
jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class
Start the BDK.
Test.
B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4

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.
B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4


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-bystep 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.
Enterprise Java Beans:
Enterprise Java Beans or EJB are server side components managed by an EJB
container.Their similarity to Java Beans that we have been studying so far is only to the
extent that they both share the name Bean and both are customizable components.A
normal Java Bean is basically an Intra-Process Component,where as an EJB is an InterProcess Component.This means that an EJB component can be accessed and used by many
processes existing in different JVMs.
B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4


Since every component has a declared set of services(or methods) that a client or
clients might invoke,they must execute atomically.This means that some kind of code
synchronization becomes essential,if ACID properties of a transaction are to be
maintained.This has a down side in terms of performance.If there are many clients
accessing a method on a Component and if that method is synchronized,then only one
client can access the method while the other clients wait.To reduce this problem, the server
side application can instatiate many components,passing references of different
components to different clients.This poses another problem.A particaular client may be
updating a field in the component through its reference,while another client may be
reading the same field through the latters reference,the two references being
different.Under this circumstance,the second client may not have the updated value of the
particular field in question.To take care of this the server side application must monitor the
data on all bean instances and update the modified value of all bean instances,should any
one client were to modify a particular field.Again,providing a private bean instance to
each client can be a load on system resources such as memory.To strike a balance between
performance and resource utilization,it becomes necessary that a limited number of bean
instances are shared among a larger number of clients,with the server side application
managing all the problems related to consistency of data across all the shared bean
insatances.Writing such a server side application can be very tediuos and error prone.
Enter the EJB container and EJB Server and presto!,all problems are
solved.An EJB Container is instantiated by the EJB server.Inside this container,a predefined
number of bean instances of different bean classes are created and kept in a bean
pool.When a client requests a particular bean instance,the container picks one of the bean
instance,initaializes it, and sends a refernce to this bean instance to the client.It does the
same thing to all the clients.The container at the same time monitors all the bean instances
and invokes call back methods on each bean instance that updates its fields,if there is a
change in any of its fields,thus maintaining consistancy and improving concurrency.A client
never accesses the bean directly,rather it queries the EJB container for a particular bean
instance.The container may provide a new bean instance to this client or send the client a
reference to an already instatiated bean,if that bean instance at that point in time is
idle.The client does not know wheteher the bean instance that it is using is dedicated or
shared.Thus the EJB container takes away the burden of maintaing ACID property of a
transaction from the programmer who develops the EJB.
An Enterprise Java Bean is written as if it is a single threaded application.In
fact,EJB classes can never be mutithreaded.They can not be threads themselves nor can
they start any new thread.It is the job of the container to manage instances of EJB classes.
In order for a container to be able to manage bean instances,EJB classes must implement
certain interfaces that contain the call back methods that a container invokes to notify the
bean instance of any change in its properties or to inform the bean instance of any specific
action that it has to take.

B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

www.jntuworld.com

www.android.jntuworld.com

www.jwjobs.net

WEB TECHNOLOGIES UNIT-4


EJBs come in two flavours.Entity Bean and Session Bean.The call back methods that are
required for these two types of beans are also different.These interfaces are
javax.ejb.EntityBean and
javax.ejb.SessionBean
The javax.ejb package is part of the J2EE(Java 2 Enterprise Edition)release which contain
many other packages required for Enterprise Applications,such as packages for
Servlets,JNDI pakage,JMS(Java Messaging Service)package etc.
Entity and Session Beans:
Entity Beans are components that represent some persistent data,usually a row in a
Database table.This means that an Entity EJB Object will have fields that correspond to
columns of a Databae table.There are two mechanisms bu which the persistence of such
data is managed by a typical EJB container.
Container managed Persistence
Bean Managed Persistence
In Container managed Persistence or CMP, the Container generates all the JDBC code
required to create,insert,update or delete rows from a databse table.
In Bean managed persistence or BMP,the Bean developer has to provide the code to
achieve this.CMP is normally prefered,though there may be situations when you have to
use a BMP bean.
Session Beans are noramlly used to control transactiopn between a client and an Entity
Bean.Session beans themselves do not represent any persistent data.However Session beans
can access database tables through JDBC calls.Again there are two types of session beans.
Stateless Session Beans
Stateful Session Beans.
Stateless session beans are shared beans that can be used by many clients.They
need not know the conversational state of any transaction.That is the bean does not
retain any state information between two successive methods calls invoked on it.This is
similar to the familiar http protocol which does not maintain any state information.
Stateful Session beans on the other hand maitains converstional state in a
transaction.Thus it retaias state information between successive method calls till the
transaction in question is completed,after which the bean is destroyed.Therefore
stateful Session Beans are not shared.Each transaction will have a unique bean
associated with it.

B. SOWJANYA RANI ASST.PROFESSOR,CSE, ADITYA ENGINEERING COLLEGE

www.jntuworld.com

You might also like