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如下:

  1. package com.lei.demo.aop.advice;
  2.  
  3. public class CustomerService {
  4.  
  5. private String name;
  6. private String url;
  7.  
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11.  
  12. public void setUrl(String url) {
  13. this.url = url;
  14. }
  15.  
  16. public void printName() {
  17. System.out.println("Customer name : " + this.name);
  18. }
  19.  
  20. public void printURL() {
  21. System.out.println("Customer website : " + this.url);
  22. }
  23.  
  24. public void printThrowException() {
  25. throw new IllegalArgumentException();
  26. }
  27.  
  28. }

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  7. <property name="name" value="LeiOOLei" />
  8. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  9. </bean>
  10.  
  11. </beans>

运行以下代码App.java:

  1. package com.lei.demo.aop.advice;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class App {
  7.  
  8. public static void main(String[] args) {
  9. ApplicationContext appContext = new ClassPathXmlApplicationContext(
  10. new String[] { "Spring-AOP-Advice.xml" });
  11.  
  12. CustomerService cust = (CustomerService) appContext.getBean("customerService");
  13.  
  14. System.out.println("*************************");
  15. cust.printName();
  16. System.out.println("*************************");
  17. cust.printURL();
  18. System.out.println("*************************");
  19. try {
  20. cust.printThrowException();
  21. } catch (Exception e) {
  22.  
  23. }
  24.  
  25. }
  26.  
  27. }

运行结果:

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

Customer name : LeiOOLei

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

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

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

1.      Before Advice

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

HijackBeforeMethod.java如下:

  1. package com.lei.demo.aop.advice;
  2.  
  3. import java.lang.reflect.Method;
  4. import org.springframework.aop.MethodBeforeAdvice;
  5.  
  6. public class HijackBeforeMethod implements MethodBeforeAdvice {
  7.  
  8. public void before(Method arg0, Object[] args, Object target)
  9. throws Throwable {
  10. System.out.println("HijackBeforeMethod : Before method hijacked!");
  11.  
  12. }
  13.  
  14. }

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

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

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

Apring-AOP-Advice.xml如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  7. <property name="name" value="LeiOOLei" />
  8. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  9. </bean>
  10.  
  11. <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
  12.  
  13. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  14. <property name="target" ref="customerService" />
  15. <property name="interceptorNames">
  16. <list>
  17. <value>hijackBeforeMethodBean</value>
  18. </list>
  19. </property>
  20. </bean>
  21.  
  22. </beans>

注意:

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

  1.   <dependency>
  2. <groupId>cglib</groupId>
  3. <artifactId>cglib</artifactId>
  4. <version>2.2.2</version>
  5. </dependency>

运行如下代码,注意代理

App.java如下

  1. package com.lei.demo.aop.advice;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class App {
  7.  
  8. public static void main(String[] args) {
  9. ApplicationContext appContext = new ClassPathXmlApplicationContext(
  10. new String[] { "Spring-AOP-Advice.xml" });
  11.  
  12. CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
  13.  
  14. System.out.println("使用Spring AOP 如下");
  15. System.out.println("*************************");
  16. cust.printName();
  17. System.out.println("*************************");
  18. cust.printURL();
  19. System.out.println("*************************");
  20.  
  21. try {
  22. cust.printThrowException();
  23. } catch (Exception e) {
  24.  
  25. }
  26.  
  27. }
  28.  
  29. }

输出结果:

使用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如下:

  1. package com.lei.demo.aop.advice;
  2.  
  3. import java.lang.reflect.Method;
  4. import org.springframework.aop.AfterReturningAdvice;
  5.  
  6. public class HijackAfterMethod implements AfterReturningAdvice {
  7.  
  8. public void afterReturning(Object returnValue, Method method, Object[] args,
  9. Object target) throws Throwable {
  10. System.out.println("HijackAfterMethod : After method hijacked!");
  11.  
  12. }
  13.  
  14. }

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  7. <property name="name" value="LeiOOLei" />
  8. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  9. </bean>
  10.  
  11. <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
  12. <bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.HijackAfterMethod" />
  13.  
  14. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  15. <property name="target" ref="customerService" />
  16. <property name="interceptorNames">
  17. <list>
  18. <value>hijackAfterMethodBean</value>
  19. </list>
  20. </property>
  21. </bean>
  22.  
  23. </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如下:

  1. package com.lei.demo.aop.advice;
  2.  
  3. import org.springframework.aop.ThrowsAdvice;
  4.  
  5. import sun.awt.SunToolkit.IllegalThreadException;
  6.  
  7. public class HijackThrowException implements ThrowsAdvice {
  8.  
  9. public void afterThrowing(IllegalArgumentException e) throws Throwable {
  10. System.out.println("HijackThrowException : Throw exception hijacked!");
  11. }
  12.  
  13. }

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  7. <property name="name" value="LeiOOLei" />
  8. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  9. </bean>
  10.  
  11. <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
  12. <bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.HijackAfterMethod" />
  13. <bean id="hijackThrowExceptionBean" class="com.lei.demo.aop.advice.HijackThrowException" />
  14.  
  15. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  16. <property name="target" ref="customerService" />
  17. <property name="interceptorNames">
  18. <list>
  19. <value>hijackThrowExceptionBean</value>
  20. </list>
  21. </property>
  22. </bean>
  23.  
  24. </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如下:

  1. package com.lei.demo.aop.advice;
  2.  
  3. import java.util.Arrays;
  4. import org.aopalliance.intercept.MethodInterceptor;
  5. import org.aopalliance.intercept.MethodInvocation;
  6.  
  7. public class HijackAroundMethod implements MethodInterceptor {
  8.  
  9. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  10. System.out.println("Method name : "
  11. + methodInvocation.getMethod().getName());
  12. System.out.println("Method arguments : "
  13. + Arrays.toString(methodInvocation.getArguments()));
  14.  
  15. // 相当于 MethodBeforeAdvice
  16. System.out.println("HijackAroundMethod : Before method hijacked!");
  17.  
  18. try {
  19. // 调用原方法,即调用CustomerService中的方法
  20. Object result = methodInvocation.proceed();
  21.  
  22. // 相当于 AfterReturningAdvice
  23. System.out.println("HijackAroundMethod : After method hijacked!");
  24.  
  25. return result;
  26.  
  27. } catch (IllegalArgumentException e) {
  28. // 相当于 ThrowsAdvice
  29. System.out.println("HijackAroundMethod : Throw exception hijacked!");
  30. throw e;
  31. }
  32. }
  33.  
  34. }

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
  7. <property name="name" value="LeiOOLei" />
  8. <property name="url" value="http://www.cnblogs.com/leiOOlei/" />
  9. </bean>
  10.  
  11. <bean id="hijackBeforeMethodBean" class="com.lei.demo.aop.advice.HijackBeforeMethod" />
  12. <bean id="hijackAfterMethodBean" class="com.lei.demo.aop.advice.HijackAfterMethod" />
  13. <bean id="hijackThrowExceptionBean" class="com.lei.demo.aop.advice.HijackThrowException" />
  14. <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" />
  15.  
  16. <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
  17. <property name="target" ref="customerService" />
  18. <property name="interceptorNames">
  19. <list>
  20. <value>hijackAroundMethodBean</value>
  21. </list>
  22. </property>
  23. </bean>
  24.  
  25. </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,即切入点和通知,以后的章节中会逐渐介绍。

Spring3系列9- Spring AOP——Advice的更多相关文章

  1. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  2. Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建

    上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...

  3. Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现

    我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...

  4. Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现

    上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...

  5. spring AOP advice 类型 和 通用的切点的配置方式

    spring aop advice的类型: 1.前置通知(before advice) 2.返回后通知(after returning advice) 3.抛出异常后通知(after throwing ...

  6. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  7. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  8. Spring5.0源码学习系列之Spring AOP简述

    前言介绍 附录:Spring源码学习专栏 在前面章节的学习中,我们对Spring框架的IOC实现源码有了一定的了解,接着本文继续学习Springframework一个核心的技术点AOP技术. 在学习S ...

  9. SSH框架系列:Spring AOP应用记录日志Demo

    分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...

  10. spring aop advice

    1.前置通知(BeforeAdvice): import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAd ...

随机推荐

  1. 个性二维码开源专题<介绍篇>

    由C#编写的个性二维码底层,已应用到 码晒客/疯狂创意二维码等项目上,并获得多项软件著作专利. 疯狂创意二维码 疯狂创意二维码是可用于生成风格独特的个性化二维码生成器,用户可以将目标信息输入到二维码生 ...

  2. ASP.NET MVC 4源码分析之如何定位控制器

    利用少有的空余时间,详细的浏览了下ASP.NET MVC 4的源代码.照着之前的步伐继续前进(虽然博客园已经存在很多大牛对MVC源码分析的博客,但是从个人出发,还是希望自己能够摸索出这些).首先有一个 ...

  3. 解决nginx反向代理缓存不起作用的问题

    昨天尝试用nginx搭建nuget镜像服务器,镜像服务器需要两个功能:1)反向代理:2)内容缓存. 用nginx做反向代理,配置非常简单,只需在/etc/nginx/nginx.conf中添加一个包含 ...

  4. git删除push到远程服务器的commit

    如果不小心把不该提交的代码或者敏感的数据(如密码)提交到远程git服务器上,可以使用git reset回滚到上一个commit,并且commit history不留下任何痕迹. 具体做法: # 1.通 ...

  5. [stm32][ucos] 1、基于ucos操作系统的LED闪烁、串口通信简单例程

    * 内容简述: 本例程操作系统采用ucos2.86a版本, 建立了5个任务            任务名                                             优先级 ...

  6. [WinAPI] API 13 [遍历指定目录 打印文件和其他属性]

    Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...

  7. error at ::0 can't find referenced pointcut解决办法(转载)

    原文:http://blog.sina.com.cn/s/blog_9ecb0d9d0101fheg.html Spring中采用annotation的方式实现AOP代理,运行测试代码时抛出以下异常: ...

  8. C语言实现二叉树-02版

    ---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...

  9. paip.花生壳 服务启动失败 以及不能安装服务,权限失败的解决

    paip.花生壳 服务启动失败 以及不能安装服务,权限失败的解决 系统win7 NewPhDDNS_1.0.0.30166.exe  作者Attilax  艾龙,  EMAIL:1466519819@ ...

  10. Activemq 平台搭建与C#示列

    ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS ...