Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了。

乍一看好像是Before advice和After advice的组合,也就是说pointcut会在joinpoint执行前后各执行一次。但是这种理解是不正确的,如果这样理解的话,就会产生这样的疑问:spring aop Around类型为什么只执行一次 ,这个帖子是我碰巧看到。

那么怎么样理解才是正确的呢?我们来看一下Spring官方是怎么解释Around advice的:

Around advice runs "around" a matched method execution. It has the opportunity to do work both before and after the method executes, and to determine when, how, and even if, the method actually gets to execute at all. Around advice is often used if you need to share state before and after a method execution in a thread-safe manner (starting and stopping a timer for example).

大概的意思就是说,Around advice可以通过一个在joinpoint执行前后做一些事情的机会,可以决定什么时候,怎么样去执行joinpoint,甚至可以决定是否真的执行joinpoint的方法调用。Around advice通常是用在下面这样的情况:

在多线程环境下,在joinpoint方法调用前后的处理中需要共享一些数据。如果使用Before advice和After advice也可以达到目的,但是就需要在aspect里面创建一个存储共享信息的field,而且这种做法并不是线程安全的。

现在,明白Spring设计Around advice的目的之后,我们来看下具体的用法。

import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.ProceedingJoinPoint;@Aspectpublic class AroundExample {@Around("com.xyz.myapp.SystemArchitecture.businessService()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {// start stopwatch   相当于是before advice
Object retVal = pjp.proceed();// stop stopwatch    相当于是after advicereturn retVal;}}

现在大家看明白了吧,并不是在joinpoint执行前后各调用一次pointcut,而是在pointcut中把joinpoint给around起来。

摘自:http://blog.163.com/chen_guangqi/blog/static/2003111492012101653052508/

转载于:https://www.cnblogs.com/csniper/p/5499248.html

正确理解Spring AOP中的Around advice相关推荐

  1. java @around_正确理解Spring AOP中的Around advice

    Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...

  2. Spring : Spring AOP 中的增强(Advice)或者通知

    1.美图 2.概述 概念参考:Spring :Spring AOP 中的一些术语 很多人将增强理解为通知,但是理解为增强会更加准确,因为它表示在连接点上执行的行为,这个行为是目标类类所没有的,是为目标 ...

  3. Spring AOP中定义切点(PointCut)和通知(Advice)

    本文讨论一下Spring AOP编程中的两个关键问题,定义切点和定义通知,理解这两个问题能应付大部分AOP场景. 如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子. ...

  4. Spring AOP 中 advice 的四种类型 before after throwing advice around

    Spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  5. 彻底理解Spring AOP

    目录 前言 1. AOP概念 2. AOP的实现 3. Spring的IoC理解: 4. Sping知识整理 前言 AOP英文名为Aspect Oriented Programming,意为面向切面编 ...

  6. 如何理解 Spring AOP 以及使用 AspectJ?

    作者 | 阿文 责编 | 屠敏 出品 | CSDN(ID:CSDNnews) 在 Spring 中 AOP 是一个非常非常重要的概念,那么什么是AOP呢? AOP 即面向切面编程,也可以叫做面向方向编 ...

  7. Spring AOP:搞清楚advice的执行顺序

    文章目录 目录 AOP的核心概念 模拟aspect advice的执行过程 同一aspect,不同advice的执行顺序 不同aspect,advice的执行顺序 同一aspect,相同advice的 ...

  8. Spring AOP中定义切点PointCut详解

    1.AOP是什么? 软件工程有一个基本原则叫做"关注点分离"(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年 ...

  9. Spring AOP中Pointcut,dvice 和 Advisor三个概念

    Spring  AOP中Pointcut,dvice 和 Advisor三个概念介绍 在理解了Spring的AOP后,需要重点理解的三个概念是:Pointcut    Advice   Advisor ...

最新文章

  1. 如何快速融入团队(二)
  2. RTX 3090 AI性能实测:FP32训练速度提升50%,张量核心缩水
  3. 沉默是金 矩阵快速幂
  4. 错误:用脚本window.showModalDialog打开的页面,点击button时打开新窗口
  5. ABAP Development Tools的语法增强
  6. 基本机器学习面试问题 --- 理论/算法2
  7. JavaWeb基础—JS学习小结
  8. 互联网企业互相屏蔽对方的链接,这种事情以后不行了!
  9. Python+tensorflow计算整数阶乘的方法与局限性
  10. python修改python unittest的运行顺序
  11. java poi excel模板变量_Java Web之POI操作Excel2016模板
  12. Ps照片一键生成彩铅马克笔手绘效果图方法
  13. Symbian OS 源码下载方式
  14. opencore amr android,苹果手机amr文件用什么打开,opencore框架进行语音
  15. 青岛科技大学软件测试专业如何,青岛科技大学优势专业排名
  16. C语言三位数分别输出个位十位百位
  17. Eclipse中设置jsp文件 字体大小
  18. 市面上主流的音视频竞品分析对比
  19. 骑行中央公园,探索纽约“后花园”别样的美
  20. Soul网关源码分析-环境搭建

热门文章

  1. 在项目开始前,为客户做专门的“需求变更流程”培训是必要的
  2. c#接口和抽象类的区别
  3. 2009暑期实践报告
  4. CSMAR 智能财经报告分析平台帮助
  5. approach for doing things
  6. if you canget up early
  7. mac实际上是非常适合编程的,我之前的认识的确是有些有限的
  8. 转载 用ShadowVolume画模型的影子
  9. windows 如何使用4GB(开启3GB和PAE)
  10. python练习程序(批量重命名)