Java Reference
In-Depth Information
LISTING 8-6 (continued)
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class SomeBusinessService {
@Interceptors(SecurityInterceptor.class)     
public void startService(){
//Complex business logic
Logger.getLogger("AppLog").info("done...");
}
public void startAnotherService(){
//Complex business logic
Logger.getLogger("AppLog").info("done again...");
}
}
This time only calls to the startService() method are intercepted, unlike in Listing 8‐5, in which
all methods of the class were intercepted. You should annotate each method separately.
Using @Interceptor , r @Interceptors with @AroundInvoke unleashes a powerful tool that
solves cross‐cutting concerns in an AOP approach. Yet interceptors offer easy annotation‐based
implementation with no boilerplate code.
You can use the InvocationContext interface to extract information about the context or interact
with the advice context. Following are some of the more useful methods:
DESCRIPTION
Return to the target advice.
public Object getTarget();
Return the executed method from the advice.
public Method getMethod();
Access target advice method's parameters.
public Object[] getParameters();
Set target advice method's parameters.
public void setParameters(Object[]);
public java.util.Map<String,Object>
getContextData();
Access context data.
public Object proceed() throws
Exception;
Continue execution.
In Listing 8‐7, you can access the method name. Also, you can check whether the interceptor had
authorized the access before; if it has not, you can authorize the user for that method.
Search WWH ::




Custom Search