Spring AOP Example

There are given samples of Spring1.2 old style AOP (dtd based) usage. In spite of the fact that it is upheld in spring 3, however it is prescribed to utilize spring aop with aspectJ that we are going to learn in next page.

There are 4 types of advices supported in spring1.2 old style aop implementation.

  1. Before Advice it is executed before the genuine method call.
  2. After Advice it is executed after the genuine method call. If method returns a value, it is executed after returning value.
  3. Around Advice it is executed before and after the genuine method call.
  4. Throws Advice it is executed if genuine method throws exception.

The hierarchy of advice interfaces

Let's understand the advice hierarchy by the diagram given below:

spring aop advice interfaces

All are interfaces in aop.

MethodBeforeAdvice interface extends the BeforeAdvice interface.

AfterReturningAdvice interface extends the AfterAdvice interface.

ThrowsAdvice interface extends the AfterAdvice interface.

MethodInterceptor interface extends the Interceptor interface. It is used in around advice.


1- MethodBeforeAdvice Example

Create a class that contains actual business logic.

A.java
package com.javatportal;
public class A {
public void m(){System.out.println("actual business logic");}
}

Now, create the advisor class that implements MethodBeforeAdvice interface.

BeforeAdvisor.java
package com.javatportal;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice{
	@Override
	public void before(Method method, Object[] args, Object target)throws Throwable {
		System.out.println("additional concern before actual logic");
	}
}

In xml file, create 3 beans, one for A class, second for Advisor class and third for ProxyFactoryBean class.

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="obj" class="com.javatportal.A"></bean>
<bean id="ba" class="com.javatportal.BeforeAdvisor"></bean>

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>

</beans>

Understanding ProxyFactoryBean class:

The ProxyFactoryBean class is provided by Spring Famework. It contains 2 properties target and interceptorNames. The instance of A class will be considered as target object and the instance of advisor class as interceptor. You need to pass the advisor object as the list object as in the xml file given above.

The ProxyFactoryBean class is written something like this:

public class ProxyFactoryBean{
private Object target;
private List interceptorNames;
//getters and setters
}

Now, let's call the actual method.

Test.java
package com.javatportal;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	
	A a=factory.getBean("proxy",A.class);
	a.m();
}
}

Output

additional concern before actual logic
actual business logic

Printing additional information in MethodBeforeAdvice

We can print additional information like method name, method argument, target object, target object class name, proxy class etc.

BeforeAdvisor.java
package com.javatportal;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvisor implements MethodBeforeAdvice{
	@Override
	public void before(Method method, Object[] args, Object target)throws Throwable {
		System.out.println("additional concern before actual logic");
		System.out.println("method info:"+method.getName()+" "+method.getModifiers());
		System.out.println("argument info:");
		for(Object arg:args)
			System.out.println(arg);
		System.out.println("target Object:"+target);
		System.out.println("target object class name: "+target.getClass().getName());
	}
}
Test.java
package com.javatportal;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	
	A a=factory.getBean("proxy",A.class);
        System.out.println("proxy class name: "+a.getClass().getName());
	a.m();
}
}

Output

proxy class name: com.javatportal.A$$EnhancerByCGLIB$$409872b1
additional concern before actual logic
method info:m 1
argument info:
target Object:com.javatportal.A@11dba45
target object class name: com.javatportal.A
actual business logic

2- AfterReturningAdvice Example

Create a class that contains actual business logic.

A.java

Same as in the previous example.

Now, create the advisor class that implements AfterReturningAdvice interface.

AfterAdvisor.java
package com.javatportal;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvisor implements AfterReturningAdvice{
	@Override
	public void afterReturning(Object returnValue, Method method,
         Object[] args, Object target) throws Throwable {
		
		System.out.println("additional concern after returning advice");
	}

}

Create the xml file as in the previous example, you need to change only the advisor class here.

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="obj" class="com.javatportal.A"></bean>
<bean id="ba" class="com.javatportal.AfterAdvisor"></bean>

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>

</beans>
Test.java

Same as in the previous example.

Output

actual business logic
additional concern after returning advice

3- MethodInterceptor (AroundAdvice) Example

Create a class that contains actual business logic.

A.java

Same as in the previous example.

Now, create the advisor class that implements MethodInterceptor interface.

AroundAdvisor.java
package com.javatportal;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvisor implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		Object obj;
		System.out.println("additional concern before actual logic");
		obj=mi.proceed();
		System.out.println("additional concern after actual logic");
		return obj;
	}

}

Create the xml file as in the previous example, you need to change only the advisor class here.

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="obj" class="com.javatportal.A"></bean>
<bean id="ba" class="com.javatportal.AroundAdvisor"></bean>

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>

</beans>
Test.java

Same as in the previous example.

Output

additional concern before actual logic
actual business logic
additional concern after actual logic

4- ThrowsAdvice Example

Create a class that contains actual business logic.

Validator.java
package com.javatportal;
public class Validator {
	public void validate(int age)throws Exception{
		if(age <18){
			throw new ArithmeticException("Not Valid Age");
		}
		else{
			System.out.println("vote confirmed");
		}
	}
}

Now, create the advisor class that implements ThrowsAdvice interface.

ThrowsAdvisor.java
package com.javatportal;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsAdvisor implements ThrowsAdvice{
	public void afterThrowing(Exception ex){
		System.out.println("additional concern if exception occurs");
	}
}

Create the xml file as in the previous example, you need to change only the Validator class and advisor class.

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="obj" class="com.javatportal.Validator"></bean>
<bean id="ba" class="com.javatportal.ThrowsAdvisor"></bean>

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="obj"></property>
<property name="interceptorNames">
<list>
<value>ba</value>
</list>
</property>
</bean>

</beans>
Test.java
package com.javatportal;

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

public class Test {
public static void main(String[] args) {
	Resource r=new ClassPathResource("applicationContext.xml");
	BeanFactory factory=new XmlBeanFactory(r);
	
	Validator v=factory.getBean("proxy",Validator.class);
	try{
	v.validate(12);
	}catch(Exception e){e.printStackTrace();}
}
}

Output

java.lang.ArithmeticException: Not Valid Age

additional concern if exception occurs

	at com.javatportal.Validator.validate(Validator.java:7)
	at com.javatportal.Validator$$FastClassByCGLIB$$562915cf.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invoke
Joinpoint(Cglib2AopProxy.java:692)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.
invoke(ThrowsAdviceInterceptor.java:124)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.
intercept(Cglib2AopProxy.java:625)
	at com.javatportal.Validator$$EnhancerByCGLIB$$4230ed28.validate(<generated>)
	at com.javatportal.Test.main(Test.java:15)
s