Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 944
Umair Avatar asked Sep 17 '13 14:09

Umair


People also ask

What is an interceptor in programming?

In the field of software development, an interceptor pattern is a software design pattern that is used when software systems or frameworks want to offer a way to change, or augment, their usual processing cycle.

What are interceptors in middleware?

The interceptor can transform data between the controller and the client-side, which can perform functions for: bind extra logic before / after method execution. transform the result returned from a function. transform the exception thrown from a function.

What are interceptors in struts?

Interceptors are conceptually the same as servlet filters or the JDKs Proxy class. Interceptors allow for crosscutting functionality to be implemented separately from the action as well as the framework. You can achieve the following using interceptors − Providing preprocessing logic before the action is called.

How do you call an interceptor in Java?

Use the @AroundInvoke annotation to designate interceptor methods for managed object methods. Only one around-invoke interceptor method per class is allowed. Around-invoke interceptor methods have the following form: @AroundInvoke visibility Object method-name(InvocationContext) throws Exception { ... }


2 Answers

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

like image 166
Arun Gupta Avatar answered Oct 02 '22 12:10

Arun Gupta


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/

like image 35
Sundararaj Govindasamy Avatar answered Oct 02 '22 10:10

Sundararaj Govindasamy