There are given examples of Spring1.2 old style AOP (dtd based) implementation.
Though it is supported in spring 3, but it is recommended to use 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.
- Before Advice it is executed before the actual method call.
- After Advice it is executed after the actual method call. If method returns a value, it is executed after returning value.
- Around Advice it is executed before and after the actual method call.
- Throws Advice it is executed if actual method throws exception.
To understand the basic concepts of Spring AOP, visit the previous page.
Understanding the hierarchy of advice interfaces
Let's understand the advice hierarchy by the diagram given below:
spring aop advice interfaces image
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.
File: A.java
package com.javahubpoint;
public class A {
public void m(){System.out.println("actual business logic");}
}
Now, create the advisor class that implements MethodBeforeAdvice interface.
File: BeforeAdvisor.java
package com.javahubpoint;
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.
File: 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.javahubpoint.A"></bean>
<bean id="ba" class="com.javahubpoint.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.
File: Test.java
package com.javahubpoint;
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.
You need to change only two classes BeforeAdvisor.java and Test.java.
File: BeforeAdvisor.java
package com.javahubpoint;
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());
}
}
File: Test.java
package com.javahubpoint;
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.javahubpoint.A$$EnhancerByCGLIB$$409872b1
additional concern before actual logic
method info:m 1
argument info:
target Object:com.javahubpoint.A@11dba45
target object class name: com.javahubpoint.A
actual business logic
2) AfterReturningAdvice Example
Create a class that contains actual business logic.
File: A.java
Same as in the previous example.
Now, create the advisor class that implements AfterReturningAdvice interface.
File: AfterAdvisor.java
package com.javahubpoint;
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.
File: 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.javahubpoint.A"></bean>
<bean id="ba" class="com.javahubpoint.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>
File: 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.
File: A.java
Same as in the previous example.
Now, create the advisor class that implements MethodInterceptor interface.
File: AroundAdvisor.java
package com.javahubpoint;
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.
File: 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.javahubpoint.A"></bean>
<bean id="ba" class="com.javahubpoint.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>
File: 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.
File: Validator.java
package com.javahubpoint;
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.
File: ThrowsAdvisor.java
package com.javahubpoint;
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.
File: 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.javahubpoint.Validator"></bean>
<bean id="ba" class="com.javahubpoint.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>
File: Test.java
package com.javahubpoint;
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.javahubpoint.Validator.validate(Validator.java:7)
at com.javahubpoint.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.javahubpoint.Validator$$EnhancerByCGLIB$$4230ed28.validate(<generated>)
at com.javahubpoint.Test.main(Test.java:15)