31 Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点。简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能。
在Spring AOP中,有 4 种类型通知(advices)的支持:
1 通知(Advice)之前 - 该方法执行前运行
2 通知(Advice)返回之后 – 运行后,该方法返回一个结果
3 通知(Advice)抛出之后 – 运行方法抛出异常后,
4 环绕通知 – 环绕方法执行运行,结合以上这三个通知。
下面的例子显示Spring AOP 通知如何工作。
简单的 Spring 例子
创建一个简单的客户服务类及一个print方法作为演示。
package com.gp6.aop.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();
}
}
<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-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.advice.CustomerService">
<property name="name" value="Gp6" />
<property name="url" value="http://www.jianshu.com/u/bdfd4b762426" />
</bean>
</beans>
package com.gp6.aop.advice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/advice/applicationContext.xml");
CustomerService cust = (CustomerService) appContext.getBean("customerService");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
}
}
}
一个简单的Spring项目用来注入(DI)bean和输出一些字符串。
Spring AOP 通知
现在,附加 Spring AOP 建议到上述的客户服务。
1. 之前通知
它会在方法执行之前执行。创建一个实现 MethodBeforeAdvice 接口的类。
package com.gp6.aop.advice.before;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HijackBeforeMethod implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method!");
}
}
在 bean 配置文件(applicationContext.xml),创建一个 bean 的 HijackBeforeMethod 类,并命名为“customerServiceProxy” 作为一个新的代理对象。
- ‘target’ – 定义你想拦截的bean。
- ‘interceptorNames’ – 定义要应用这个代理/目标对象的类(通知)。
package com.gp6.aop.advice.before;
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();
}
}
package com.gp6.aop.advice.before;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HijackBeforeMethod implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method!");
}
}
<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-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.advice.before.CustomerService">
<property name="name" value="Gp6" />
<property name="url" value="http://www.jianshu.com/u/bdfd4b762426" />
</bean>
<bean id="hijackBeforeMethodBean" class="com.gp6.aop.advice.before.HijackBeforeMethod" />
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackBeforeMethodBean</value>
</list>
</property>
</bean>
</beans>
package com.gp6.aop.advice.before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/advice/before/applicationContext.xml");
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
}
}
}
输出结果
*************************
HijackBeforeMethod : Before method!
Customer name : Gp6
*************************
HijackBeforeMethod : Before method!
Customer website : http://www.jianshu.com/u/bdfd4b762426
*************************
HijackBeforeMethod : Before method!
它将运行 HijackBeforeMethod 的 before() 方法,在每个 CustomerService 的方法之前执行。
2.返回后通知
该方法返回一个结果之后它将执行。创建一个实现AfterReturningAdvice接口的类。
package com.gp6.aop.advice.after;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class HijackAfterMethod implements AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!");
}
}
package com.gp6.aop.advice.after;
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();
}
}
<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-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.advice.after.CustomerService">
<property name="name" value="Gp6" />
<property name="url" value="http://www.jianshu.com/u/bdfd4b762426" />
</bean>
<bean id="hijackAfterMethodBean" class="com.gp6.aop.advice.after.HijackAfterMethod" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackAfterMethodBean</value>
</list>
</property>
</bean>
</beans>
package com.gp6.aop.advice.after;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/advice/after/applicationContext.xml");
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
}
}
}
再次运行,输出
*************************
Customer name : Gp6
HijackAfterMethod : After method hijacked!
*************************
Customer website : http://www.jianshu.com/u/bdfd4b762426
HijackAfterMethod : After method hijacked!
*************************
它将运行 HijackAfterMethod 的 afterReturning()方法,在每次 CustomerService 方法返回结果之后。
3.抛出后通知
它将在执行方法抛出一个异常后。创建一个实现ThrowsAdvice接口的类,并创建一个afterThrowing方法拦截抛出:IllegalArgumentException异常。
package com.gp6.aop.advice.afterThrow;
import org.springframework.aop.ThrowsAdvice;
public class HijackThrowException implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
}
}
<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-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.advice.afterThrow.CustomerService">
<property name="name" value="Gp6" />
<property name="url" value="http://www.jianshu.com/u/bdfd4b762426" />
</bean>
<bean id="hijackThrowExceptionBean" class="com.gp6.aop.advice.afterThrow.HijackThrowException" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackThrowExceptionBean</value>
</list>
</property>
</bean>
</beans>
package com.gp6.aop.advice.afterThrow;
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();
}
}
package com.gp6.aop.advice.afterThrow;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/advice/afterThrow/applicationContext.xml");
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
System.out.println("扎心不");
cust.printThrowException();
} catch (Exception e) {
}
}
}
输出:
*************************
Customer name : Gp6
*************************
Customer website : http://www.jianshu.com/u/bdfd4b762426
*************************
扎心不
HijackThrowException : Throw exception hijacked!
它将运行 HijackThrowException 的 afterThrowing()方法,如果 CustomerService 的方法抛出异常。
4.环绕通知
它结合了上面的三个通知,在方法执行过程中执行。创建一个实现了MethodInterceptor接口的类。必须调用“methodInvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。
package com.gp6.aop.advice.around;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HijackAroundMethod 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("HijackAroundMethod : Before method hijacked!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("HijackAroundMethod : Before after hijacked!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}
<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-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.advice.around.CustomerService">
<property name="name" value="Gp6" />
<property name="url" value="http://www.jianshu.com/u/bdfd4b762426" />
</bean>
<bean id="hijackAroundMethodBean" class="com.gp6.aop.advice.around.HijackAroundMethod" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackAroundMethodBean</value>
</list>
</property>
</bean>
</beans>
package com.gp6.aop.advice.around;
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();
}
}
package com.gp6.aop.advice.around;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/advice/around/applicationContext.xml");
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
}
}
}
它将运行HijackAroundMethod 的 invoke()方法,在每一个 CustomerService 方法执行后。
总结
大部分的 Spring 开发者都只是实现了“环绕通知”,因为它可以对所有通知类型,但更好的做法应该是选择最合适的通知类型来满足要求。
在这个例子中,在一客户服务类中的所有方法都自动拦截(通知)。但在大多数情况下,可能需要使用切入点和Advisor通过它的方法名拦截它的方法。