Spring3系列9- Spring AOP——Advice

时间:2021-10-13 20:05:44

Spring3系列9- Spring AOP——Advice

  Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统中分布于各个模块(不同方法)中的交叉关注点的问题。简单地说,就是一个拦截器(interceptor)拦截一些处理过程。例如,当一个method被执行,Spring AOP能够劫持正在运行的method,在method执行前或者后加入一些额外的功能。

在Spring AOP中,支持4中类型的通知(Advice)

Before advice      ——method执行前通知

After returning advice ——method返回一个结果后通知

After throwing advice – method抛出异常后通知

Around advice – 环绕通知,结合了以上三种

下边这个例子解释Spring AOP怎样工作。

首先一个简单的不使用AOP的例子。

先创建一个简单的Service,为了稍后演示,这个类中加了几个简单的打印method。

CustomerService.java如下:

package com.lei.demo.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();
} }

Xml配置文件Apring-AOP-Advice.xml如下:

<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> </beans>

运行以下代码App.java:

package com.lei.demo.aop.advice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "Spring-AOP-Advice.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) { } } }

运行结果:

*************************

Customer name : LeiOOLei

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

1.      Before Advice

创建一个实现了接口MethodBeforeAdvice的class,method运行前,将运行下边的代码

HijackBeforeMethod.java如下:

package com.lei.demo.aop.advice;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice; public class HijackBeforeMethod implements MethodBeforeAdvice { public void before(Method arg0, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method hijacked!"); } }

在配置文件中加入新的bean配置HijackBeforeMethod,然后创建一个新的代理(proxy),命名为customerServiceProxy。

“target”定义你想劫持哪个bean;

“interceptorNames”定义你想用哪个class(advice)劫持target。

Apring-AOP-Advice.xml如下:

<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.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>

注意:

用Spring proxy之前,必须添加CGLIB2类库,,以下是pom.xml依赖

  <dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>

运行如下代码,注意代理

App.java如下

package com.lei.demo.aop.advice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "Spring-AOP-Advice.xml" }); CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy"); System.out.println("使用Spring AOP 如下");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************"); try {
cust.printThrowException();
} catch (Exception e) { } } }

输出结果:

使用Spring AOP 如下

*************************

HijackBeforeMethod : Before method hijacked!

Customer name : LeiOOLei

*************************

HijackBeforeMethod : Before method hijacked!

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

HijackBeforeMethod : Before method hijacked!

每一个customerService的method运行前,都将先执行HijackBeforeMethod的before方法。

2.      After Returning Advice

创建一个实现了接口AfterReturningAdvice的class,method运行后,直到返回结果后,才运行下边的代码,如果没有返回结果,将不运行切入的代码。

HijackAfterMethod.java如下:

package com.lei.demo.aop.advice;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice; public class HijackAfterMethod implements AfterReturningAdvice { public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!"); } }

修改bean配置文件,加入hijackAfterMethodBean配置,Apring-AOP-Advice.xml如下:

<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
<bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.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>

现在再运行App.java后输出如下:

使用Spring AOP 如下

*************************

Customer name : LeiOOLei

HijackAfterMethod : After method hijacked!

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

HijackAfterMethod : After method hijacked!

*************************

可以看到输出结果,每一个customerService的method运行返回结果后,都将再执行HijackAfterMethod的afterReturning方法。但是执行到cust.printThrowException()后,直接抛出异常,方法没有正常执行完毕(或者说没有返回结果),所以不运行切入的afterReturning方法。

3.      Afetr Throwing Advice

创建一个实现了ThrowsAdvice接口的class,劫持IllegalArgumentException异常,目标method运行时,抛出IllegalArgumentException异常后,运行切入的方法。

HijackThrowException.java如下:

package com.lei.demo.aop.advice;

import org.springframework.aop.ThrowsAdvice;

import sun.awt.SunToolkit.IllegalThreadException;

public class HijackThrowException implements ThrowsAdvice {

    public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
} }

修改bean配置文件,加入了hijackThrowExceptionBean,Apring-AOP-Advice.xml如下:

<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
<bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.HijackAfterMethod" />
<bean id="hijackThrowExceptionBean" class="com.lei.demo.aop.advice.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>

运行结果如下:

使用Spring AOP 如下

*************************

Customer name : LeiOOLei

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

HijackThrowException : Throw exception hijacked!

当运行CustomerService中的printThrowException方法时,认为的抛出IllegalArgumentException异常,被HijackThrowException截获,运行其中的afterThrowing方法。注意,如果抛出异常不是IllegalArgumentException,则不能被截获。

4.      Around Advice

结合了以上3种形式的Advice,创建一个实现了接口MethodInterceptor的class,你必须通过methodInvocation.proceed()来调用原来的方法,即通过调用methodInvocation.proceed()来调用CustomerService中的每一个方法,当然也可以不调用原方法。

HijackAroundMethod.java如下:

package com.lei.demo.aop.advice;

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class HijackAroundMethod implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments())); // 相当于 MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!"); try {
// 调用原方法,即调用CustomerService中的方法
Object result = methodInvocation.proceed(); // 相当于 AfterReturningAdvice
System.out.println("HijackAroundMethod : After method hijacked!"); return result; } catch (IllegalArgumentException e) {
// 相当于 ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
} }

修改bean配置文件,加入了hijackAroundMethodBean,Apring-AOP-Advice.xml如下:

<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.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
<bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.HijackAfterMethod" />
<bean id="hijackThrowExceptionBean" class="com.lei.demo.aop.advice.HijackThrowException" />
<bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.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>

执行App.java,输出结果:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Method name : printURL

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer website : http://www.cnblogs.com/leiOOlei/

HijackAroundMethod : After method hijacked!

*************************

Method name : printThrowException

Method arguments : []

HijackAroundMethod : Before method hijacked!

HijackAroundMethod : Throw exception hijacked!

CustomerService中每一个方法的调用,都会执行HijackAroundMethod中的invoke方法,可以看到整个切入点将目标around。

大多数的Spring开发者只用Around Advice,因为它能够实现所有类型的Advice。在实际的项目开发中,我们还是要尽量选择适合的Advice。

在以上的例子中,CustomerService中的所有方法都被自动拦截,但是大多数情况下,我们不需要拦截一个class中的所有方法,而是拦截符合条件的方法。这时,我们就需要用到Pointcut and Advice,即切入点和通知,以后的章节中会逐渐介绍。