1.4. What's new

1.4.1. Metadata annotations

Metadata annotations is new : To define a stateless session bean, the @Stateless annotation is declared on the bean class.

1.4.2. Business interceptors

Interceptors are new in EJB3. It's allow the developper to intercept each business method of the bean. The parameters can be changed and the returned values too. In order to know the time that a method takes for its execution, it can be done with this kind of interceptor.

1.4.3. Lifecycle interceptors

In addition to business interceptors, the EJB2 callbacks, like ejbActivate() method, are now defined by using annotation. For ejbActivate() method, this is done with the help of @PostActivate annotation. This annotation is set on a method which will be called by the container.

1.4.4. Dependency injection

Dependency injection is a new feature. It allows to request that the container inject resources instead of trying to get them. For example, with the previous specification, in order to get an EJB, the following code was used :

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

Now, this is done by using only this code :

@EJB private MyInterface myBean;

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

1.4.5. Persistence

New things are linked to the persistence layer. For example EJB3 entities are POJO (Plain Old Java Object). It means that they can be created by using the new() constructor : new MyEntity();

Also entities are managed by an EntityManager. ie : entitymanager.persist(entity);

Entities have callbacks available too.