Features of EJB 3.0 :

This article talks about the various new features introduced in the Enterprise JavaBean (EJB) 3.0 specification and how they'll make developing EJBs easier.


Using Annotations Instead of Deployment Descriptors :

EJB 3.0 uses metadata annotations as an alternative to deployment descriptors.For example, to define a stateless session bean, the @Stateless annotation is declared on the bean class.So a developer marks up his/her Java code with annotations and the annotation processor creates the deployment descriptors at runtime. For example, the following code shows how to define a simple stateless session bean:

This article talks about the various new features introduced in the Enterprise JavaBean (EJB) 3.0 specification and how they'll make developing EJBs easier.


Using Annotations Instead of Deployment Descriptors :

EJB 3.0 uses metadata annotations as an alternative to deployment descriptors.For example, to define a stateless session bean, the @Stateless annotation is declared on the bean class.So a developer marks up his/her Java code with annotations and the annotation processor creates the deployment descriptors at runtime. For example, the following code shows how to define a simple stateless session bean:




Example for Stateless Session Bean
@Stateless
public class HelloBean implements Hello{
    public String sayHello(){
        return "Hello Boss";
    }
}

Example for Business Interface
@Remote
public interface Hello{
    public String getMessage();
}

The @Stateless annotation marks the HelloBean as a stateless session bean.And, the @Remote annotation indicates that it's a business interface. But it is not mandatory to write Remote interface, we can use @Remote in the Bean class itself.For example, the following code shows how to define a simple stateless session bean in which @Remote annotation is used in Bean Class Itself.


Business Interface
public interface Hello{
    public String sayHello();
}

The Business interface is simple POJI which contains method declaration sayHello().


Bean Class
@Stateless
@Remote(Hello.class)
public class HelloBean implements Hello{
    public String sayHello(){
        return "Hello Boss";
    }
}

In the above code, the bean class implement the business interface, and uses @Remote annotation in Bean Class instead of declaring it in component interface.

Notice the two annotation @Stateless and @Remote. @Stateless annotation in Bean class configures the Bean as stateless Session Bean,whereas @Remote annotation configures the bean to support Remote client via the interface Hello.

Consider the Bean Class given below,here the bean class doesn't implement the business interface,instead of that it used @Remote annotation.Here the bean class forces the container to generate the business interface. EJB 3.0 provides annotations for every type of metadata previously addressed by deployment descriptors, so no XML descriptor is needed

Bean Class
@Stateless
@Remote
public class HelloBean{
    public String sayHello(){
        return "Hello Boss";
    }
}

EJB 3.0 provides annotations for every type of metadata previously addressed by deployment descriptors, so no XML descriptor is needed.

Note:- XML has not been completely removed,it's now optional If we define both a deployment descriptor and annotations, the deployment descriptor overrides the annotations.



Dependency Injection :

Dependency injection is the mechanism through which the container injects the requested environmental entry s, instead of trying to get them.For example, with the EJB2 specification, in order to get an EJB, the following code was used:

try {
Object o = new InitialContext().lookup("java:comp/env/ejb/HelloEJB");
myBean = PortableRemoteObject.narrow(o, HelloInterface.clas);
} catch (NamingException e) {
....
}

With EJB3 this could be done by following simple code:

@EJB
private HelloInterface helloBean;

If the @EJB annotation is found in the class, the container will look up and inject an instance of the bean in the helloBean variable.

Callback Methods and Listener Classes :

In EJB 3.0, bean developers do not have to implement unnecessary callback methods(ejbCreate(), ejbPassivate(), and ejbActivate() etc). Developer can declare any method as a callback method to receive notifications for lifecycle events for a SessionBean or MessageDrivenBean(MDB). Callback methods can be indicated using callback annotations.

Developers can now just implement the callbacks they are interested in.

Apart from that, we can design a callback listener class instead of writing callback methods in the bean class. The annotations used for callback methods are the same in both cases--only the method signatures are different.A callback method defined in a listener class must take a Object as a parameter.

Defining a callback method in the Bean Class :

In the below code fragment, we are defining a callback method PreDestroy in the bean class. @PreDestroy is one of the callback annotations.

@Stateful
public class MyBean{
    private int x;
    public int m1(){}
    @PreDestroy 
	call(){}
}

Defining a callback method in the Listener Class :

In the below code fragment, we are defining a callback method in the Listener Class.

/* Callback method defined inside a Listener class*/
public class CustomCallBack{
    @PrePassivate public customCall(Object obj){
        // Statements
    }
}
 
/* Adds callback listener to bean class */
@CallbackListener CustomCallBack
@stateful
public class MyBean{
    private int x;
    public int m1(){}
}

Here, we are writing a separate listener class for callback methods and have a small difference in method signatures. We must pass an Object parameter, because we're defining the callback listener in a separate class. A bean class adds the callback listener class by using a special callback annotation @CallbackListener.

Interceptor :

EJB 3.0 interceptors allow you to add functionality to business methods of your session bean without modifying the methods code. An interceptor method will execute before any of the bean's business methods are invoked.Interceptors can be used on stateless session beans, stateful session beans, and message driven beans.

Persistence :

New features are linked to the persistence layer. For example, EJB3 entities are POJO (Plain Old Java Object).




comments powered by Disqus