These next sections will delve deeper into interceptor binding types to explain more advanced use cases of them. We will cover binding types with members, how to combine different interceptors for a single use, and how binding types can be inherited.
Just as with qualifier annotations, we can also add members to interceptor binding types too, such as:
@InterceptorBinding @Target( { METHOD, TYPE } ) @Retention( RUNTIME ) public @interface Audited { boolean logToFile() default false; }
With the binding type we just created, we need to create a new interceptor that matches the situation of logToFile
being true
, as CDI uses the member as a means for choosing a different interceptor implementation. For our preceding interceptor binding type, the implementation would be:
@Audited( logToFile = true ) @Interceptor public class AuditFileInterceptor { @AroundInvoke public Object auditMethod(InvocationContext ctx) throws Exception ...