Java Reference
In-Depth Information
.info(context.getMethod().getName()+ "is accessed!");
        return context.proceed();
    }
}
To put the interceptor class into action, you must annotate the target advice with the
@Interceptors annotation as in Listing 8‐5. The @Interceptors annotation would only be used in
an EJB or MDB (Message Driven Bean).
LISTING 8‐5: Simple implementation of target advice
package com.devchronicles.interceptor;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
@Interceptors(SecurityInterceptor.class)
@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...");
}
}
The Interceptors annotation is l exible. You can also use it at both the class and the method
levels. The Interceptors annotation also supports multiple interceptor inputs, which enable
multiple interceptors on the target advice. Listing 8‐5 uses class‐level interceptors, which means
that the Security interceptor will intercept either of the service calls. If you do not want the
interceptor to cover all method calls in the class, you can use method‐level annotations, as
shown in Listing 8‐6.
LISTING 8‐6: Implementation of class‐level interceptors
package com.devchronicles.interceptor;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
continues
Search WWH ::




Custom Search