Intercepting Lifecycle Events
Not only can you intercept EJB method invocations, but you can
also intercept EJB lifecycle events. These callbacks can be used to
initialize the state of your EJB bean classes, as well as the interceptor
class itself. Lifecycle interception looks very similar to the @AroundInvoke style:
@callback-annotation> voidmethod-name(InvocationContext ctx);
To intercept an EJB callback, define a method within your
interceptor class that is annotated with the callback in which you are
interested. The return value of the method must be void because EJB callbacks have no return value.
The method name can be anything and must not throw any checked exceptions
(no throws clause). InvocationContext is the only parameter to this
method. As with @AroundInvoke methods,
callback interception is invoked in one big Java call stack. This means
you must call InvocationContext.proceed() to complete the
lifecycle event. When calling proceed(), the next interceptor class that has
the same callback is invoked. If there are no other interceptors, then the
callback method of the EJB’s bean class is invoked, if one exists. If the
EJB has no callback method, then proceed() is a no-op. Because there may be no
callback method, InvocationContext.getMethod() always returns
null.
Custom Injection Annotations
Why would you want to intercept an EJB callback? One concrete example is when you want to create and define your own injection annotations. The EJB specification has a bunch of annotations ...