JSR-299 CDI Interceptors

Markus Eisele
Playing around with Java EE 6 these days, I came across some new features, I will blog about. Today I just want to give a short introduction to the enhancements made to javax.interceptor by the CDI.

Interceptor functionality is allready defined by the Java Interceptors specification. CDI enhances this with a more sophisticated, annotation-based approach for binding interceptors to beans. Only four stepts to get to your new CDI interceptor.

1) Write the interceptor binding:

@Target({METHOD, TYPE})
public @interface Log {}

2) Write the interceptor:

public class LoggingInterceptor {

public Object logExecutionTime(InvocationContext ic) throws Exception {
long start = System.currentTimeMillis();
try {
return ic.proceed();
} catch (Exception e) {
throw e;
} finally {
long time = System.currentTimeMillis() - start;
String method = ic.getClass().getName();
.log(Level.INFO, "*** Invocation of "
+ method + " took " + time + "ms");

3) Declare the interceptor in beans.xml:

4) Use the interceptor in your code:

public String getText() {

That is all. Have fun.

Post a Comment


  1. Nice, perhaps a better link to the javax.interceptor package:


  2. Shouldn't annotation be called LogTime intead of Log in step 1?

Post a Comment