Spring AOP即Aspect-oriented programming,面向切面编程,是做为面向对象编程的一种补充,专门用于处理系统中分布于各个模块(不一样方法)中的交叉关注点的问题。简单地说,就是一个拦截器(interceptor)拦截一些处理过程。例如,当一个method被执行,Spring AOP可以劫持正在运行的method,在method执行前或者后加入一些额外的功能。java
在Spring AOP中,支持4中类型的通知(Advice)web
Before advice ——method执行前通知spring
After returning advice ——method返回一个结果后通知编程
After throwing advice – method抛出异常后通知app
Around advice – 环绕通知,结合了以上三种this
下边这个例子解释Spring AOP怎样工做。url
首先一个简单的不使用AOP的例子。spa
先建立一个简单的Service,为了稍后演示,这个类中加了几个简单的打印method。代理
CustomerService.java以下:code
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/
*************************
建立一个实现了接口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方法。
建立一个实现了接口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方法。
建立一个实现了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,则不能被截获。
结合了以上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,即切入点和通知,之后的章节中会逐渐介绍。