What are Interceptors in Java EE?

JavaJakarta EeInterceptor

Java Problem Overview


I'm trying to clear my concept about Interceptors in Java EE. I have read Java EE specification but I'm little confused about it. Please provide me some useful link or tutorial which could clear my concept. How, When, Why do we use interceptors?

Java Solutions


Solution 1 - Java

Interceptors are used to implement cross-cutting concerns, such as logging, auditing, and security, from the business logic.

In Java EE 5, Interceptors were allowed only on EJBs. In Java EE 6, Interceptors became a new specification of its own, abstracted at a higher level so that it can be more generically applied to a broader set of specifications in the platform.

They intercept invocations and life-cycle events on an associated target class. Basically, an interceptor is a class whose methods are invoked when business methods on a target class are invoked, life-cycle events such as methods that create/destroy the bean occur, or an EJB timeout method occurs. The CDI specification defines a type-safe mechanism for associating interceptors to beans using interceptor bindings.

Look for a working code sample at:

https://github.com/arun-gupta/javaee7-samples/tree/master/cdi/interceptors

Java EE 7 also introduced a new @Transactional annotation in Java Transaction API. This allows you to have container-managed transactions outside an EJB. This annotation is defined as an interceptor binding and implemented by the Java EE runtime. A working sample of @Transactional is at:

https://github.com/arun-gupta/javaee7-samples/tree/master/jta/transaction-scope

Solution 2 - Java

Interceptors are used to add AOP capability to managed beans.

We can attach Interceptor to our class using @Interceptor annotation. Whenever a method in our class is called, the attached Interceptor will intercept that method invocation and execute its interceptor method.

This can be achieved using @AroundInvoke annotation ( see example below ).

Method Interceptors

We can intercept life cycle events of a class ( object creation,destroy etc) using @AroundConstruct annotation.

Main difference between Interceptor and Servlet Filters is We can use Interceptor outside WebContext, but Filters are specific to Web applications.

Common uses of interceptors are logging, auditing, and profiling.

For more detailed introduction, you can read this article. https://abhirockzz.wordpress.com/2015/01/03/java-ee-interceptors/

Solution 3 - Java

I like this definition: Interceptors are components that intercept calls to EJB methods. They can be used for auditing and logging as and when EJBs are accessed.

In another situation, they can be used in a situation where we need to check whether a client has the authority or clearance to execute a transaction on a particular object in the database. Well, this is where Interceptors come in handy; they can check whether the client/user has that authority by checking whether he/she can invoke that method on that database object or EJB.

However, I would still have a look at the following article and the following tutorial to get an idea of how they are used in a Java EE setting/environment.

Solution 4 - Java

You can use Interceptor for the places where you want to perform some tasks before sending a request to the Controller class and before sending a request to a responce. Ex:- Think you want to validate the auth token before sending a request to the Controller class in this case, you can use Intercepters. Sample code:

@Component
public class AuthInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler) throws Exception {
    //Here you can write code to validate the auth token.
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUmairView Question on Stackoverflow
Solution 1 - JavaArun GuptaView Answer on Stackoverflow
Solution 2 - JavaSundararaj GovindasamyView Answer on Stackoverflow
Solution 3 - JavablackpantherView Answer on Stackoverflow
Solution 4 - JavaRuwan PrasadView Answer on Stackoverflow