Package javax.interceptor, annotations on intercepting method
@AroundConstruct - call back after construction of target class
Must call Invocation.proceed, or target instance not created
Construct sequence:
DI completed for all interceptors associated with target class
@AroundConstruct method called
all associated @AroundConstruct interceptor called Invocation.proceed
target class created
target class constructor injection performed
@PostConstruct called
@AroundInvoke
@AroundInvoke
public void interceptOrder(InvocationContext ctx) {...}
@AroundTimeout - interposing on timeout methods for enterprise bean timers
@AroundTimeout
protected void timeoutInterceptorMethod(InvocationContext ctx) { ... }
@PostConstruct (allows only one)
@PostConstruct
void initialize() { ... }
@PreDestroy (allows only one)
@PreDestroy
void cleanup(InvocationContext ctx) { ... }
@Interceptor (optional, not required to be annotated)
default constructor
may be target for DI
have the same lifecycle as the associated target class
class annotate with interceptor type
method annotate with interceptor annotation
one class may be declare multiple interceptor binding types
multiple interceptor class may declare one interceptor binding type
@Logged
@Interceptor
public class LoggingInterceptor {
@AroundInvoke
public Object logInvocation(InvocationContext ctx) throws Exception { ... }
...
}
@Interceptors({PrimaryInterceptor.class, SecondaryInterceptor.class, LastInterceptor.class})
public void updateInfo(String info) { ... }
same InterceptorContext is passed to each interceptor, its contextData can be used to share data
InterceptorContext.parameters
Define interceptor binding type
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
pubic @interface Logged { ... }
Interceptor can be applied to another interceptor binding type
@Logged
@InterceptorBinding
@Target({TYPE, METHOD}) // CONSTRUCTOR
@Retention(RUNTIME)
@Inherited
public @interface Secured { ... }
defined in deployment descriptor (XML) first
@Interceptors invoked by order of listing
super class invoked first than sub class
@Priority set sequence, smaller value invoked first