Metadata annotations is new. For example, to define a stateless
session bean, the @Stateless
annotation is declared
on the bean class.
The new business interceptors allow the developer to intercept each business method of the bean. The parameters and the returned values can be changed. For example, an interceptor can be used to determine the time that a method takes to execute.
In addition to business interceptors, the EJB2 callbacks ( such as
the ejbActivate()
method) are now defined using
annotation. For the ejbActivate()
method, this
is done with the help of @PostActivate
annotation.
This annotation is set on a method that will be called by the
container.
Dependency injection makes it possible to request that the container inject resources, 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/MyEJB"); myBean = PortableRemoteObject.narrow(o, MyInterface.clas); } catch (NamingException e) { .... }
With EJB3 this is done using only the following code:
@EJB private MyInterface myBean;
If the @EJB
annotation is found in the class,
the container will look up and inject an instance of the bean in the
myBean
variable.
New features are linked to the persistence layer. For example,
EJB3 entities are POJO (Plain Old Java Object). This means that they can
be created by using the new()
constructor:
new MyEntity();
Also
entities are managed by an
EntityManager: entitymanager.persist(entity);
In addition, entities have callbacks available.