Java Reference
In-Depth Information
@Audited @Interceptor
public class AuditInterceptor {
@AroundInvoke
public Object audit(InvocationContext context) throws Exception {
System.out.print("Invoking: "
+ context.getMethod().getName());
System.out.println(" with arguments: "
+ context.getParameters());
return context.proceed();
}
}
The @Interceptor annotation declares this class to be an interceptor. The @Audited
annotation links this interceptor to your interceptor binding. All CDI interceptors require
an interceptor binding. If you were using regular EJB interceptors (see Ac-
tionBazaarLogger in listing 5.10 ) , then you wouldn't need these annotations. Then,
of course, there's the familiar @AroundInvoke annotation that tells CDI what method of
this class will be called when the interceptor is invoked. Now that you have an interceptor
binding and you've applied the binding to an interceptor, it's time to finish by linking the
interceptor to an EJB.
Linking an interceptor to a bean
To link an interceptor to an EJB, you use the interceptor binding either on the bean class
or on methods within the bean. Suppose BidService of the ActionBazaar application
needs to be audited. You can easily audit all methods of the EJB by annotating the class:
@Stateless
@Audited
public class BidService {
...
}
Or you can audit just the addBid() method:
@Stateless
public class BidService {
@Audited
public void addBid(Bid bid) {
bidDao.addBid(bid);
}
...
}
Search WWH ::




Custom Search