简体   繁体   中英

What are Interceptors in Java EE?

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?

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

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.

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 ).

方法拦截器

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/

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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM