Spring AOP
Spring AOP[Aspect-oriented programming] is one of the important feature of spring framework, used to modularize cross-cutting concerns in aspects. Aspects is just an interceptor to intercept some processes, for example, when a method is execute, Spring AOP can intercept in between the executing method, and add extra functionality before or after the method execution.
In Spring AOP there are 4 type of advices are as under:
- Before advice Run before the method execution
- After returning advice Run after the method returns a result
- After throwing advice Run after the method throws an exception
- Around advice Run around the method execution, combine all three advices above.
Spring AOP Before Advice Example
Student.java File
package com.jtechies.bo;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void printThrowException() {
throw new IllegalArgumentException();
}
}
BeforeAdvise.java File
package com.jtechies.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvise implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args,
Object target)throws Throwable {
System.out.println("BeforeAdvise is running...");
}
}
Test.java File
package com.jtechies.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support
.ClassPathXmlApplicationContext;
import com.jtechies.bo.Student;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new
ClassPathXmlApplicationContext(
new String[] { "spring.xml" });
Student student = (Student)
appContext.getBean("studentServiceProxy");
System.out.println("---------------------------");
System.out.println
("Student name = "+ student.getName());
System.out.println("---------------------------");
System.out.println
("Student age = "+student.getAge());
System.out.println("---------------------------");
try {
student.printThrowException();
} catch (Exception e) {
}
}
}
spring.xml File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="com.jtechies.bo.Student">
<property name="name" value="peter"/>
<property name="age" value="25"/>
</bean>
<bean id="before" class="com.jtechies.aop.BeforeAdvise">
<bean id="studentServiceProxy"
class="org.springframework.aop.framework
.ProxyFactoryBean">
<property name="target" ref="student"/>
<property name="interceptorNames">
<list>
<value>before</value>
</list>
</property>
</bean>
</beans>
Output
************************* BeforeAdvise is running... Student name = peter ************************* BeforeAdvise is running... Student age = 25 ************************* BeforeAdvise is running...
Spring AOP After returning Advice Example
AfterMethod.java File
package com.jtechies.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterMethod implements AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue,
Method method, Object[] args,Object target)
throws Throwable {
System.out.println
("AfterReturningAdvice is running...");
}
}
spring.xml File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="com.jtechies.bo.Student">
<property name="name" value="peter"/>
<property name="age" value="25"/>
</bean>
<bean id="afterReturn"
class="com.jtechies.aop.AfterMethod">
<bean id="studentServiceProxy"
class="org.springframework.aop.framework
.ProxyFactoryBean">
<property name="target" ref="student"/>
<property name="interceptorNames">
<list>
<value>afterReturn</value>
</list>
</property>
</bean>
</beans>
Output
----------------------------- Student name = peter AfterReturningAdvice is running... ----------------------------- Student age = 25 AfterReturningAdvice is running... -----------------------------
Spring AOP After throwing advice Example
ThrowsMethod.java File
package com.jtechies.aop;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsMethod implements ThrowsAdvice{
public void afterThrowing(IllegalArgumentException e)
throws Throwable {
System.out.println("ThrowsMethod is running...!");
}
}
spring.xml File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="com.jtechies.bo.Student">
<property name="name" value="peter"/>
<property name="age" value="25"/>
</bean>
<bean id="throwsMethod"
class="com.jtechies.aop.ThrowsMethod">
<bean id="studentServiceProxy"
class="org.springframework.aop.framework
.ProxyFactoryBean">
<property name="target" ref="student"/>
<property name="interceptorNames">
<list>
<value>throwsMethod</value>
</list>
</property>
</bean>
</beans>
Output
----------------------------- Student name = peter ----------------------------- Student age = 25 ----------------------------- ThrowsMethod is running...!
Spring AOP Around advice Example
ThrowsMethod.java File
package com.jtechies.aop;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke
(MethodInvocation methodInvocation)throws Throwable {
System.out.println("Method name : "+
methodInvocation.getMethod().getName());
System.out.println("Method arguments : "+
Arrays.toString
(methodInvocation.getArguments()));
// same with MethodBeforeAdvice
System.out.println("Before advice is running...");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println
("Returning is running...");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println
("ThrowsMethod is running...");
throw e;
}
}
}
spring.xml File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="com.jtechies.bo.Student">
<property name="name" value="peter"/>
<property name="age" value="25"/>
</bean>
<bean id="aroundAdvice"
class="com.jtechies.aop.AroundAdvice">
<bean id="studentServiceProxy"
class="org.springframework.aop.framework
.ProxyFactoryBean">
<property name="target" ref="student"/>
<property name="interceptorNames">
<list>
<value>aroundAdvice</value>
</list>
</property>
</bean>
</beans>
Output
----------------------------- Method name : getName Method arguments : [] Before advice is running... Returning is running... Student name = peter ----------------------------- Method name : getAge Method arguments : [] Before advice is running... Returning is running... Student age = 25 ----------------------------- Method name : printThrowException Method arguments : [] Before advice is running... ThrowsMethod is running...



