Spring in Action 4 读书笔记之使用标签创建 AOP
2017-01-23 18:43
501 查看
目录
1. 定义一个aspect
2. 创建一个
around advice
在之前的读书笔记 Spring
in Acton 4 读书笔记之 AOP 原理及 Spring 对 AOP 的支持 中,讲到 Spring 对 AOP 的支持包含四方面:
Spring 基于代理的经典的 AOP
使用 XML 配置将纯 POJO 转化为 aspect
使用 Spring 的 @Aspect 标签,创建 aspect
Spring 不创建 aspect,只注入(引用)AspectJ 框架的 aspect
Spring in Action(Spring 实战)的第四章第三节(4.3 Creating annotated aspects)讲述了其中第三种,即如何使用标签创建 aspect。本文讲解其中的前面两小节:定义 aspect 以及创建 around advice。
AspectJ 5 引进的主要特性是使用标签创建 aspect。AspectJ 5 的缺点是需要学习扩展的 java 语言。但是 AspectJ 面向标签的编程模式使得将一个类转换成 aspect 变得很容易,只需要在类中加一些标签。
定义一个
aspect
以下是使用标签创建 AOP 的例子,这个例子在之前的文章中有提到过,观众在演出开始前后,以及出问题时,会自动做出一些反应:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package concert; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class Audience { @Before("execution(** concert.Performance.perform(..))") public void silenceCellPhones() { System.out.println("Silencing cell phones"); } @Before("execution(** concert.Performance.perform(..))") public void takeSeats() { System.out.println("Taking seats"); } @AfterReturning("execution(** concert.Performance.perform(..))") public void applause() {  System.out.println("CLAP CLAP CLAP!!!"); } @AfterThrowing("execution(** concert.Performance.perform(..))") public void demandRefund() { System.out.println("Demanding a refund"); } } |
表演结束后,观众需要鼓掌(applause()),如果表演过程中出现了异常,观众会要求退票(demandRefund())。AspectJ 提供了五个标签来定义 advice:
@After,在方法正常执行结束,或者出现异常的时候,执行 aspect。
@AfterReturning,在方法正常执行结束后,执行 aspect。
@AfterThrowing,在方法抛出异常的时候,执行 aspect。
@Around,在方法执行过程中,执行 aspect。
@Before, 在方法执行之前,执行 aspect。
上面的例子中,所有标签的值都是一个 pointcut 表达式,而且在这个例子里,正好是一样的(因为是作用在同一个方法上)。实际上,可以将这个 pointcut 定义好,然后进行引用,这样可以避免重复编写 pointcut。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @Aspect public class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {}  @Before("performance()") public void silenceCellPhones() { System.out.println("Silencing cell phones"); } @Before("performance()") public void takeSeats() { System.out.println("Taking seats"); } @AfterReturning("performance()") public void applause() { System.out.println("CLAP CLAP CLAP!!!"); } @AfterThrowing("performance()") public void demandRefund() { System.out.println("Demanding a refund"); } } |
如果只是定义了上面 Audience 这个 aspect, 那么其实什么也做不了。必须有一个配置文件,指出它是一个 aspect,并且解析这些标签,然后创建代理,最终将 Audience 转化为一个 aspect。
如果是使用 JavaConfig, 可以在配置文件类加上 @EnableAspectJAutoProxy 标签,以实现自动代理。以下是示例:
1 2 3 4 5 6 7 8 9 | @Configuration @EnableAspectJAutoProxy @ComponentScan public class ConcertConfig { @Bean public Audience audience() { return new Audience(); } } |
创建一个
around advice
前面讲解了 before 和 after 的用法,由于 around 的用法有些不同,也更有用,所以这里单讲。先看看下面的例子:1 2 3 4 5 6 7 8 910 | @Aspect public class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } } } |
需要注意的是,必须执行 joinpoint 的 proceed()方法,否则,会导致被监听的方法没有执行。
我计划完成 50 到 100 篇关于 Spring 的文章,这是第十一篇,欢迎订阅 tantanit.com,第一时间获取文章更新,本文链接地址:http://tantanit.com/springinaction4-du-shu-bi-ji-zhi-shi-yong-biao-qian-chuang-jian-aop/
谈谈 IT的文章均为原创或翻译(翻译会注明外文来源),转载请以链接形式标明本文地址: http://tantanit.com/springinaction4-du-shu-bi-ji-zhi-shi-yong-biao-qian-chuang-jian-aop/
from: http://tantanit.com/springinaction4-du-shu-bi-ji-zhi-shi-yong-biao-qian-chuang-jian-aop/
相关文章推荐
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>
- Spring in Action(第二版)中文版读书笔记(2-1)
- Spring in action 读书笔记
- 《Spring In action》学习笔记——AOP(面向切面编程)
- 使用spring的aop实现拦截action后出现依赖注入为空的问题
- SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP
- Spring In Action(第三版)读书笔记
- Spring in Action 学习笔记—第三章创建切面
- Spring in Action 读书笔记 第七章 SpringMVC
- spring in action 读书笔记
- SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍
- Spring Aop 例子(Spring in action)
- 使用Struts2的标签报“警告: No configuration found for the specified action: 'login.action' in ...”的处理方法
- spring in action 读书笔记
- Spring In Action(中文第二版)读书笔记(1-1)
- 读书笔记:spring in action
- Spring in Action 入门之面向切面编程AOP
- struts2 与 spring 使用aop处理Action异常
- Spring in action 学习(2)创建Bean
- Spring in Action 学习笔记三-AOP