This is Fourth part of CDI discussion.Please visit part one, part two and part three
Interceptors
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)
Log.java
here @InterceptorBinding is represent that the annotation 'Log' is interceptor
second, we need to implement the interceptors
LogImpl.java
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
SimeClass.java
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
Interceptors
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)
Log.java
package org.ramki.interceptor;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
/**
*
* @author Ramakrishnan
*/
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Log {
}
here @InterceptorBinding is represent that the annotation 'Log' is interceptor
second, we need to implement the interceptors
LogImpl.java
package org.ramki.interceptor.impl;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.ramki.interceptor.Log;
/**
*
* @author Ramakrishnan
*/
@Interceptor
@Log
public class LogImpl {
@AroundInvoke
public Object logging(InvocationContext context) throws Exception
{
System.out.println("Log before method call...");
Object returnObject = context.proceed();
// to do some logging
System.out.println("Log after method call...");
return returnObject;
}
}
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
SimeClass.java
package org.ramki.beans;
import org.ramki.interceptor.Log;
import org.ramki.interceptor.Time;
/**
*
* @author Ramakrishnan
*/
@Log
public class SomeClass {
public void methodA() {
System.out.println("indide methodA...");
}
public void methodB() {
System.out.println("indide methodB...");
}
public void methodC() {
System.out.println("indide methodC...");
}
public void methodD() {
System.out.println("indide methodD...");
}
public void methodE() {
System.out.println("indide methodE...");
}
}
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
org.ramki.interceptor.impl.LogImpl
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