Thursday, October 13, 2011

Short description of Spring AOP.


AOP stand for Aspect Oriented Programming 

Aspect - It is concept which a developer wants to implements globally i.e. Think of this as the general feature you want to apply globally to your application (logging etc).
Advice - Code that is used to implement Aspect. A chunk of code that is invoked during program execution, and is a piece of the logic for implementing your aspect.
Joinpoint - A single location in the code where an advice should be executed (such as field access, method invocation , constructor invocation, etc.).
Pointcut - A pointcut is a set of many joinpoints where an advice should be executed.
Targets/Target Objects - The objects you want to apply an aspect or set of aspects to
Introduction - This is the ability to add methods to an object.

Lets take a simple example


==Interface
package springExample;

public interface Adder {

    public int add(int a,int b);

}

==Implementation class

package springExample;

public class AdderImpl implements Adder {

    public int add(int a, int b){
        return a+b;
    }

}
==Execution class

package springExample;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;

public class AdderTest {

    public static void main(String args[]){

        Resource resource = new FileSystemResource("src/SpringHelloWorld.xml");    
        BeanFactory factory = new XmlBeanFactory(resource);
       // XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
        Adder adder = (Adder)factory.getBean("adder");
        int result = adder.add(10,11);
        System.out.println("Result = " + result);

        result = adder.add(0,0);
        System.out.println("Result = " + result);
    }
}
==AfterAdvice
package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
public class LogAfterReturningAdvice implements AfterReturningAdvice{
    public void afterReturning(Object returnValue, Method method, Object[] args,
    Object target) throws Throwable {
        System.out.println("After Normal Return from Method");
    }
}
==ThrowAdvice
package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
public class LogAfterThrowsAdvice implements ThrowsAdvice{
    public void afterThrowing(Method method, Object[] args, Object target,
    Exception exception){
        System.out.println("Exception is thrown on method " + method.getName());
    }
}
==Around Advice

package springExample;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.aopalliance.intercept.MethodInvocation;

public class LogAroundAdvice implements MethodInterceptor{
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Object arguments[] = methodInvocation.getArguments();
        int number1 = ((Integer)arguments[0]).intValue();
        int number2 = ((Integer)arguments[1]).intValue();
        if (number1 == 0 && number2 == 0){
            throw new Exception("Dont know how to add 0 and 0!!!");
        }
        return methodInvocation.proceed();
    }

@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
// TODO Auto-generated method stub
return null;
}
}
==Before Advice

package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
public class LogBeforeCallAdvice implements MethodBeforeAdvice
{
    public void before(Method method, Object[] args, Object target) {
        System.out.println("Before Calling the Method>>>>>>>>>>>..");
        System.out.println("method>>>>>>>>>>>>>>"+method.getName());
        for(int i=0;i
        {
        System.out.println("args>>>>>>>>>>>>>>"+args[i].toString());
        }
        System.out.println("target>>>>>>>>>>>>>>" + target.getClass().getName());
    }
}
===XML




===========Out Put

Before Calling the Method>>>>>>>>>>>..
method>>>>>>>>>>>>>>add
args>>>>>>>>>>>>>>10
args>>>>>>>>>>>>>>11
target>>>>>>>>>>>>>>springExample.AdderImpl
After Normal Return from Method
Result = 21
Before Calling the Method>>>>>>>>>>>..
method>>>>>>>>>>>>>>add
args>>>>>>>>>>>>>>0
args>>>>>>>>>>>>>>0
target>>>>>>>>>>>>>>springExample.AdderImpl
After Normal Return from Method
Result = 0



No comments: