December 21, 2014

Java EE 6 Interceptors 1.0 (JSR318)

Introduction

Interceptor was in EE 5 introduced in EJB 3.0, see EJB3 Interceptors javax.ejb.AroundInvoke. In EE 6 that was taken out and made generic into Interceptors (JSR318), package javax.interceptor.*.

You can call Interceptor in EE 6 in two way:

  • By annotating method or class with @javax.interceptor.Interceptors(MyInterceptor.class)
  • Create custom Annotation with Annotation @javax.interceptor.InterceptorBinding and annotate with that you method or class.

Example

Your custom Interceptor.

Interception in a POJO.

Interception in a Stateless Session Bean.

beans.xml located for war in WEB-INF/.

To use the Interceptor you must let weld create the intercepted class, otherwise will weld never knew about the interceptor and hence will the interceptor never work.

Example usage from a Servlet.

Limitation

You cannot use Interceptor directly in servlets, see https://java.net/projects/servlet-spec/lists/jsr340-experts/archive/2012-02/message/0.

No comments: