Java Reference
In-Depth Information
import javax.interceptor.Interceptors;
@ExcludeDefaultInterceptors
@ExcludeClassInterceptors
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class SomeBusinessService {
public void startService(){
//Complex business logic
Logger.getLogger("AppLog").info("done...");
}
public void startAnotherService(){
//Complex business logic
Logger.getLogger("AppLog").info("done again...");
}
}
Still, the example given is only valid in EJB and MDBs, which may not be enough for all cases.
Thanks to CDI, it is not hard to achieve more.
CDI Interceptors
Before CDI, interceptors were applicable only for EJB and MDBs. CDI unleashed a huge power and
transformed interceptors into an AOP‐capable feature that works on any object.
Implementing CDI interceptors is straightforward and quite l exible. First, you need to specify a
binding. A binding is a custom annotation annotated with @InterceptorBinding .
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface Secure {}
The @InterceptorBinding is used to bind interceptors with the target code. Next, you can
implement and annotate the interceptor with the custom binding. CDI interceptors are implemented
the same way as the EJB interceptors, the only signii cant difference being the use of the binding
annotation which can be seen in Listing 8‐10
LISTING 8‐10: Binding an Interceptor with @Secure
package com.devchronicles.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
@Secure
@Interceptor
public class SecurityInterceptor {
@AroundInvoke
public Object doSecurityCheck(InvocationContext context) throws Exception{
continues
 
Search WWH ::




Custom Search