Monday, December 21, 2009

Using Interceptors with OpenWebBeans

OpenWebBeans has a good support for interceptors. Interceptors are generally used for handling cross-cutting concerns of the application. OpenWebBeans supports 2 different interceptors configuration:
  1. Using "javax.interceptor.Interceptors" based,
  2. Using "javax.interceptor.Interceptor" with "javax.interceptor.InterceptorBinding" (This is defined by JSR-299),
  3. Use both of them. (InterceptorBinding based interceptors are called after calling Interceptors.)
You can use any of them within your web or standalone application. For example,

@Interceptors(LogInterceptor.class)
@RequestScoped
public class  Login{}
public class LogInterceptor {   
     @AroundInvoke
     public Object log(InvocationContext context) throws Exception{}
}

OR
@Log
@RequestScoped
public class Login{....} with interceptor class and interceptor binding type@Log.

@Log  @Interceptor
public class LogInterceptor{
     @AroundInvoke
      public Object log(InvocationContext context) throws Exception{}

}

To use Interceptor Binding Type version, you have to define interceptor class in a "beans.xml" file of your application.

<beans>
   <interceptors>
     <class>LoginInterceptor</class>
   </interceptors>
</beans>

The good news is that you can even use interceptors in a pure Java SE or Java Web application :)

Lightweight Dependency Injection <--> OpenWebBeans

PS: Currently OpenWebBeans does not support interceptors for @Dependent scoped beans! But it is under active development and included with the next version!

Stay tune!
--Gurkan

No comments: