One of the new EJB specification is the third version of interceptors through which you can easily use the built system of aspect-oriented programming paradigm.
in EJB3 interceptors can refer both to the life cycle of beans and their business methods. Implements to be in the form of methods. Methods-interceptors could be placed in classes of grains as well as base classes of these grains, but also can be placed in separate classes (class interceptors). Methods-interceptors can have any level of access (public
,
package,
protected, private
), But may not be for it or
static nor final
. It is worth noting that the implementation interceptor is no more limited, because they can cause JNDI, JMS, other beans, JDBC, and EntityManager'a.
first InvocationContext
method-interceptor to operate effectively must be the primary context for the call, ie, an instance of the grain, information about the called business method and its parameters, and call the result. These data can be obtained by the object InvocationContext
, which is passed as a parameter to the method-interceptor. deserves special attention
InvocationContext.proceed method ()
. Her call to go to the next interceptor control, or (if it is called in the last interceptorze in the chain) to the grain business method (if it is business interceptor methods). second Class interceptors interceptors
class is a simple class (do not even need to be marked with specific reference), which simply has to define a method-interceptors. These classes must have a public constructor bezargumentowy.lifecycle interceptors is closely connected with the life cycle grain to which it is assigned.
Class interceptors can be assigned to the whole grain or beans business methods through individual endorsement
@ Interceptors
, which takes as argument a list of interceptor classes. In practice, joining the class interceptor looks something like this: @ Stateless
@ Interceptors (pl.dwalczak.Interceptor1.class) public class Seed {... @ Interceptors (pl.dwalczak.Interceptor2.class) public void metodaBiznesowa () {}}
third Lifecycle interceptors
grains to the interceptor method has been life-cycle grains should be determined one of the following statements, according to the desired stage of life: - @ PostConstruct
- @ PostActivate
- @ PreDestroy
- @ PrePassivate
If the method is defined in the class of grain, it should have the following signature: void \u0026lt;METHOD> ()other hand, if there is a class interceptors it should look like tat:
void \u0026lt;METHOD> (InvocationContext)
4th Interceptors
business methods to the method was the interceptor for business methods, it should mark the annotation @ AroundInvoke
. In one class interceptor, beans, grains or base class that can be annotated to mark only one method, but this does not lead to a reduction to a business method of grain was only one interceptor. All interceptors defined in parent classes, interceptorach connected through the @ Interceptors annotation
and that defined in the class of grain will be effect, and the sequence of their implementation will comply with the order of their declarations and the class inheritance hierarchy of the grain. methods annotated @ AroundInvoke
are performed on the call stack at samycm business method. This method should return the value returned by the method of business, and be able to throw an exception thrown by the method of business. For this reason, should have the following signature: Object \u0026lt;METHOD> (InvocationContext) throws Exception Interceptorto pass control further, ie to the next interceptor, or business methods, you should call the
proceed on an object InvocationContext
. 5th Support Resources
EJB3 specificationThe Interceptor Pattern