Now that we have the interceptor binding type, we need to implement the interceptor. All that's required is to create a standard interceptor and annotate it with @Interceptor
and our interceptor binding type of @Audited
:
@Audited @Interceptor public class AuditInterceptor { @AroundInvoke public Object auditMethod(InvocationContext ctx) throws Exception { ... } }
CDI interceptors are also able to take full advantage of dependency injection like all beans in our application:
@Audited @Interceptor public class AuditInterceptor { @Inject AuditService service; @AroundInvoke public Object auditMethod(InvocationContext ctx) throws Exception { ... } }
Now we're ready to enable our interceptor for our application. As all interceptors are disabled by default, we need to enable our interceptor within the beans.xml
file of a bean archive:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance...