Understanding CDI (Contexts and Dependency Injection) Part 4

This is Fourth  part of CDI discussion.Please visit part onepart two and  part three

Interceptors is one feature of CDI. Using this feature we can intercept the method call. Its counterpart technique for Aspect Oriented Programming (AOP) in Spring. Its helps to analysis the business methods and these interceptors are disabled by default. so we can enable/disable the interceptor in deployment time through beans.xml  file.

Create interceptor
     To create interceptor involves two step process. first to create interceptor binding and  implement the interceptors.

here we create Log Interceptor, so we need to create Log Interceptor Binding(its  like qualifier)


here @InterceptorBinding is represent that the annotation 'Log' is interceptor

second, we need to implement the interceptors


here @Interceptor represent that its interceptor implementation
@Log specifies to which interceptor  implementation , both of these lines shows that this  class is Log interceptor Implementation

@AroundInvoke is comes before one method defines
that method must pass the below method signature

         public    Object    <methodName> (InvocationContext context)

here methodName is any name we can give.
in that method call  context.proceed() - to call actual method

so when we call any intercepted method then the container calls this method and here do pre-processing then call actual method through  context.proceed() and finally we can do some post-processing...

Apply Interceptor
Once interceptor is ready, then we can apply any business class

here i put @Log in class level. we can put method level also (i.e we can put some methods )

Interceptor is disabled by default. so we need to enable through beans.xml

Through servlet/jsp if u try to call business method of SomeClass class, its do all pre and post processing through interceptors.

Comments are welcomed 

Scree cast
please watch video in HQ mode

Comments are welcomed