Creating and enabling an interceptor

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 ...

Get JBoss Weld CDI for Java Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.