Enterprise Java

Java EE Interceptors

History

I think it’s important to take a look at the evolution of Interceptors in Java EE because of the simple fact that it started as an EJB-specific item and later evolved into a separate spec which is now open for extension by other Java EE specifications.

Version 1.0

Interceptors were first introduced in EJB 3.0 (part of Java EE 5). Interceptors did not have a dedicated spec but they were versioned 1.0 and bought basic AOP related features to managed beans (POJOs) via simple annotations:

  • @AroundInvoke – to annotate methods containing the interception logic for target class methods
  • @Intercerptors – to bind the the interceptor classes with their target classes/methods
  • Capability to configure interceptors for an entire module (EJB JAR) via the deployment descriptor
  • @ExcludeDefaultInterceptors – to mute default interceptors defined in the deployment descriptor
  • @ExcludeClassInterceptors – to mute a globally defined (class level) interceptor for a particular method/constructor of the class

Interceptors 1.1

Along came Java EE 6 with EJB 3.1 – Interceptors 1.1 was still included in the EJB spec document:

  • @InterceptorBinding – a type safe way of specifying interceptors of a class or a method. Please note that this annotation was leveraged by CDI 1.0 (another specification introduced in Java EE 6) and its details are present in the CDI 1.0 spec doc rather than EJB 3.1 (light bulb moment … at least for me)
  • @Interceptor – Used to explicitly declare a class containing an interception logic in a specific method (annotated with @AroundInvoke etc) as an interceptor along with an appropriate Interceptor Binding. This too was mentioned in the CDI 1.0 documentation only.
  • @AroundTimeout – used to intercept time outs of EJB timers along with a way to obtain an instance of the Timer being intercepted (via javax.interceptor.InvocationContext.getTimer())

Interceptors 1.2

Interceptors were split off into an individual spec in Java EE 7 and thus Interceptors 1.2 came into being:

  • Interceptors 1.2 was a maintenance release on top of 1.1 and hence the JSR number still remained the same as EJB 3.1 (JSR 318)
  • Interceptor.Priority (static class) – to provide capability to define the order (priority) in which the interceptors need to invoked.
  • @AroundConstruct – used to intercept the construction of the target class i.e. invoke logic prior to the constructor of the target class is invoked

It’s important to bear in mind that Interceptors are applicable to managed beans in general. Managed Beans themselves are simple POJOs which are privileged to basic services by the container – Interceptors are one of them along with life cycle callbacks, resource injection.

Memory Aid

It’s helpful to think of Interceptors as components which can interpose on beans throughout their life cycle:

  • before they are even constructed – @AroundConstruct
  • after they are constructed – @PostConstruct
  • during their life time (method invocation) – @AroundInvoke
  • prior to destruction – @PreDestroy
  • time outs of EJBs – @AroundTimeout

Let’s look at some of the traits of Interceptors in more detail and try to answer questions like:

  • where are they applied and what do they intercept ?
  • how to bind interceptors to the target (class) they are supposed to intercept ?

Interceptors Types (based on the intercepted component)

Method Interceptors

  • Achieved by @AroundInvoke
  • The method containing the logic can be part of separate class as well as the target class (class to be intercepted) itself.

Lifecycle Callback interceptors

  • Decorate the method with @AroundConstruct in order to intercept the constructor invocation for a class
  • The method annotated with @AroundConstruct cannot be a part of the intercepted class. It has to be defined using a separate Interceptor class
  • Use the @PostConstruct annotation on a method in order to intercept a call back method on a managed bean. Just to clarify again – the Interceptor spec does not define a new annotation as such. One needs to reuse the @PostConstruct (part of the Common Annotations spec) on the interceptor method.
  • The @PreDestroy (another call back annotation defined in Common Annotations spec) annotation is used in a similar fashion

Time-out Interceptors

  • As mentioned above – @AroundTimeout used to intercept time outs of EJB timers along with a way to obtain an instance of the Timer being intercepted (via javax.interceptor.InvocationContext.getTimer())

Applying/Binding Interceptors

Using @Interceptors

  • As shown in above examples – just use the @Interceptors annotation to specify the interceptor classes
  • @Interceptors can be applied on a class level (automatically applicable to all the methods of a class), to a particular method or multiple methods and constructor in case of a constructor specific interceptor using @AroundConstruct

Using @IntercerptorBinding

  • Interceptor Bindings (explained above) – Use @IntercerptorBinding annotation to define a binding annotation which is further used on the interceptor class as well as the target class (whose method, constructor etc needs to be intercepted)

Deployment Descriptor

One can also use deployment descriptors to bind interceptors and target classes either in an explicit fashion as well as in override mode to annotations.

This was a rather quick overview of Java EE interceptors. Hopefully the right trigger for you to dig deeper !

Reference: Java EE Interceptors from our JCG partner Abhishek Gupta at the Object Oriented.. blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Halil Karakose
9 years ago

What about “beans.xml”? Do we not need a “beans.xml” file to enable CDI?

J.Z.
J.Z.
9 years ago
Reply to  Halil Karakose

In CDI 1.1 beans.xml is not required, but only beans with scope annotations will be managed by CDI container.

Back to top button