-
通知(Advice)之前 - 该方法执行前运行
- 通知(Advice)返回之后 – 运行后,该方法返回一个结果
- 通知(Advice)抛出之后 – 运行方法抛出异常后,
- 环绕通知 – 环绕方法执行运行,结合以上这三个通知。
public class CustomerService {
private String name;
private String url;
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public void printName() {
System.out.println("Customer name : " + this.name);
}
public void printURL() {
System.out.println("Customer website : " + this.url);
}
public void printThrowException() {
throw new IllegalArgumentException();
}
}
File : beans.xml – 一个bean配置文件
<?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.xsd">
<bean id="customerService" class="com.ray.customer.service.CustomerService">
<property name="name" value="Ray"/>
<property name="url" value="http://www.baidu.com"/>
</bean>
</beans>
执行
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
CustomerService cust = (CustomerService) context.getBean("customerService");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
}catch(Exception e) {
}
}
}
输出
*************************
Customer name : Ray
*************************
Customer website : http://www.baidu.com
*************************
一个简单的Spring项目用来注入(DI)bean和输出一些字符串。
Spring AOP 通知
1. 之前通知(Before Advice)
public class BeforeMethod implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("BeforeMethod : Before!");
}
}
- ‘target’ – 定义你想拦截的bean。
- ‘interceptorNames’ – 定义要应用这个代理/目标对象的类(通知)。
<?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.xsd">
<bean id="customerService" class="com.ray.customer.service.CustomerService">
<property name="name" value="Ray"/>
<property name="url" value="http://www.baidu.com"/>
</bean>
<bean id="BeforeMethodBean" class="com.ray.aop.BeforeMethod"/>
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
<list>
<value>BeforeMethodBean</value>
</list>
</property>
</bean>
</beans>
再次运行它,现在得到新的 customerServiceProxy bean,而不是原来的CustomerService bean。
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
CustomerService cust = (CustomerService) context.getBean("customerServiceProxy");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
}catch(Exception e) {
}
}
}
输出结果
*************************
BeforeMethod : Before method!
Customer name : Ray
*************************
BeforeMethod : Before method!
Customer website : http://www.baidu.com
*************************
BeforeMethod : Before method!
它将运行 BeforeMethod 的 before() 方法,在每个 CustomerService 的方法之前执行。
public class AfterMethod implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!");
}
}
bean配置文件
<?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.xsd">
<bean id="customerService" class="com.ray.customer.service.CustomerService">
<property name="name" value="Ray"/>
<property name="url" value="http://www.baidu.com"/>
</bean>
<bean id="AfterMethodBean" class="com.ray.aop.AfterMethod"/>
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
<list>
<value>hAfterMethodBean</value>
</list>
</property>
</bean>
</beans>
再次运行,输出结果
*************************
Customer name : Ray
AfterMethod : After method!
*************************
Customer website : http://www.baidu.com
AfterMethod : After method!
*************************
它将运行 AfterMethod 的 afterReturning()方法,在每次 CustomerService 方法返回结果之后。
可以看到输出结果,每一个customerService的method运行返回结果后,都将再执行AfterMethod的afterReturning方法。但是执行到cust.printThrowException()后,直接抛出异常,方法没有正常执行完毕(或者说没有返回结果),所以不运行切入的afterReturning方法。
public class ThrowException implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e)throws Throwable{
System.out.println("ThrowException : Throw exception!");
}
}
bean配置文件
<?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.xsd">
<bean id="customerService" class="com.ray.customer.service.CustomerService">
<property name="name" value="Ray"/>
<property name="url" value="http://www.baidu.com"/>
</bean>
<bean id="ThrowExceptionBean" class="com.ray.aop.ThrowException"/>
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
<list>
<value>ThrowExceptionBean</value>
</list>
</property>
</bean>
</beans>
再次运行,输出结果
*************************
Customer name : Ray
*************************
Customer website : http://www.baidu.com
*************************
ThrowException : Throw exception!
它将运行 ThrowException 的 afterThrowing()方法,如果 CustomerService 的方法抛出异常。
当运行CustomerService中的printThrowException方法时,认为的抛出IllegalArgumentException异常,被ThrowException截获,运行其中的afterThrowing方法。注意,如果抛出异常不是IllegalArgumentException,则不能被截获。
它结合了上面的三个通知,在方法执行过程中执行。创建一个实现了MethodInterceptor接口的类。必须调用“methodInvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。
public class AroundMethod 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("AroundMethod : Before method!");
try {
//proceed to original method call
Object result = methodInvocation.proceed();
//same with AfterReturningAdvice
System.out.println("AroundMethod : Before after!");
return result;
}catch(IllegalArgumentException e) {
//same with ThrowsAdvice
System.out.println("AroundMethod : Throw exception!");
throw e;
}
}
}
bean配置文件
<?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.xsd">
<bean id="customerService" class="com.ray.customer.service.CustomerService">
<property name="name" value="Ray"/>
<property name="url" value="http://www.baidu.com"/>
</bean>
<bean id="AroundMethodBean" class="com.ray.aop.AroundMethod"/>
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService"/>
<property name="interceptorNames">
<list>
<value>AroundMethodBean</value>
</list>
</property>
</bean>
</beans>
再次运行,输出结果
*************************
Method name:printName
Method arguments:[]
AroundMethod : Before method!
Customer name : Ray
AroundMethod : Before after!
*************************
Method name:printURL
Method arguments:[]
AroundMethod : Before method!
Customer website : http://www.baidu.com
AroundMethod : Before after!
*************************
Method name:printThrowException
Method arguments:[]
AroundMethod : Before method!
AroundMethod : Throw exception!