0% found this document useful (0 votes)
7 views4 pages

JavaBeans

Uploaded by

26cssohar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

JavaBeans

Uploaded by

26cssohar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaBean

A JavaBean is a Java class that should follow the following conventions:

o It should have a no-arg constructor.


o It should be Serializable.

It should provide methods to set and get the values of the properties, known as getter
and setter methods.

Why use JavaBean?


It is a reusable software component. A bean encapsulates many objects into one object
so that we can access this object from multiple places. Moreover, it provides easy
maintenance.

Simple example of JavaBean class


//Employee.java

package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}

How to access the JavaBean class?


To access the JavaBean class, we should use getter and setter methods.

package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}

JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the object.
The feature can be of any Java data type, containing the classes that you define.

A JavaBean property may be read, write, read-only, or write-only. JavaBean features


are accessed through two methods in the JavaBean's implementation class:

1. getPropertyName ()

For example, if the property name is firstName, the method name would be
getFirstName() to read that property. This method is called the accessor.

2. setPropertyName ()

For example, if the property name is firstName, the method name would be
setFirstName() to write that property. This method is called the mutator.

Advantages of JavaBean
The following are the advantages of JavaBean:/p>

o The JavaBean properties and methods can be exposed to another application.


o It provides an easiness to reuse the software components.

Disadvantages of JavaBean
The following are the disadvantages of JavaBean:

o JavaBeans are mutable. So, it can't take advantages of immutable objects.


o Creating the setter and getter method for each property separately may lead to
the boilerplate code.
Comparison of session and entity beans
Session bean Entity bean
Represents a single conversation with a Typically, encapsulates persistent business
client. data—for example, a row in a database.
Typically, encapsulates an action or actions
to be taken on business data.
Is relatively short-lived. Is relatively long-lived.
Is created and used by a single client. May be shared by multiple clients.
Has no primary key. Has a primary key, which enables an
instance to be found and shared by more
than one client.
Typically, persists only for the life of the Persists beyond the life of a client instance.
conversation with the client. (However, may Persistence can be container-managed or
choose to save information.) bean-managed.
Is not recoverable—if the EJB server fails, it Is recoverable—it survives failures of the EJB
may be destroyed. server.
May be stateful (that is, have a client-specific Is typically stateful.
state) or stateless (have no non-transient
state).
May or may not be transactional. If May or may not be transactional. Must use
transactional, can manage its own OTS the container-managed transaction model.
transactions, or use container-managed
transactions. If transactional, its state is automatically
rolled back on transaction rollback.
A stateful session bean that manages its own
transactions can begin an OTS transaction in
one method and commit or roll it back in a
subsequent method.

A stateless session bean that manages its


own transactions and begins an OTS
transaction must commit (or roll back) the
transaction in the same method in which it
was started.

The state of a transactional, stateful session


bean is not automatically rolled back on
transaction rollback. In some cases, the bean
can use session synchronization to react to
syncpoint.

Is not re-entrant. May be re-entrant.


Stateless Session Bean:
 Stateless session bean do not maintain state across method calls and
transactions
The EJB server transparently reuses instances of the Bean to service different
clients at the per-method level (access to the session bean is serialized and is 1
client per session bean per method.
 Used mainly to provide a pool of beans to handle frequent but brief requests.
The EJB server transparently reuses instances of the bean to service different
clients.
 Do not retain client information from one method invocation to the next. So
many require the client to maintain on the client side which can mean more
complex client code.
 Client passes needed information as parameters to the business methods.
Performance can be improved due to fewer connections across the network.
Stateful Session Bean :
 A stateful session bean holds the client session’s state.
 It is an extension of the client that creates it.
 Its fields contain a conversational state on behalf of the session object’s client.
This state describes the conversation represented by a specific client/session
object pair.
 lifetime of these are controlled by the client and cannot be shared between
clients.

You might also like