正确理解Spring AOP中的Around advice
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;
@Aspect
public 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 advice
return retVal;
}
}
现在大家看明白了吧,并不是在joinpoint执行前后各调用一次pointcut,而是在pointcut中把joinpoint给around起来。
摘自:http://blog.163.com/chen_guangqi/blog/static/2003111492012101653052508/
正确理解Spring AOP中的Around advice的更多相关文章
- spring 理解Spring AOP 一个简单的约定游戏
应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...
- Spring AOP 中 advice 的四种类型 before after throwing advice around
spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
- Spring AOP中定义切点(PointCut)和通知(Advice)
如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...
- Spring AOP中的动态代理
0 前言 1 动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2 Spring AOP中的动态代理机制 2.1 ...
- 轻松理解 Spring AOP
目录 Spring AOP 简介 Spring AOP 的基本概念 面向切面编程 AOP 的目的 AOP 术语和流程 术语 流程 五大通知执行顺序 例子 图例 实际的代码 使用 Spring AOP ...
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...
- spring aop中pointcut表达式完整版
spring aop中pointcut表达式完整版 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @with ...
- Spring AOP中的JDK和CGLib动态代理哪个效率更高?
一.背景 今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib动态代理哪个效率更高? 二.基本概念 首先,我们知道Spring AOP的底层实现有两种方式:一种是JDK动态代理, ...
- 深入理解Spring AOP之二代理对象生成
深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...
随机推荐
- CentOS 6.5中linux grub修复
在使用Linux的过程中,难免会出现开机提示grub >而无法启动,可能是系统中/boot/grub文件丢失等原因造成的,当出现此问题的时候只要系统分区没有格式化一般是可以修复的,下面就以虚拟 ...
- Android设备唯一性判断
前段时间项目需要一个功能,就是在操作完某一个逻辑之后返回给客户一个红包,安全校验团队需要我们提供android设备的唯一标示,起初直接通过获取设备的imei号传给了server端,后台公司云迹监控发现 ...
- .Net操作注册表--un
C#操作注册表 导入命名空间 Using MicroSoft.Win32;//64位系统装的64位版本
- js对cookie的操作,包括增,取,删
在其他人都开会到时间里,我偷偷摸哦的试了一下cookie,唉,从来没有用过cookie,慢慢的知识忘光了,还好这次偷偷摸摸的做出来了,虚,大家都别出声啊 <!DOCTYPE html> & ...
- CodeForces 313C Ilya and Matrix
Ilya and Matrix Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- Cheatsheet: 2013 09.22 ~ 09.30
Other Python basics summary Another article about big O notation Mobile Getting Started with PhoneGa ...
- TortoiseGit中push的使用
https://tortoisegit.org/docs/tortoisegit/tgit-dug-push.html Options Force (May discard known changes ...
- SqlServer 一些操作
//查询一个表中的某字段为条件修改另一个表的内容 update [VehicleInsuranceAgentConfiguration] set Keyword2=PC.ServerConfig fr ...
- JPG 批量压缩、 PNG32、PNG24转PNG 透明批量压缩工具 【JPNG】 支持多级目录
说在最前,压缩不一定是最好的,仅仅是为了方便自己工作需要.主要是手机端图片 算法说明:JPG压缩使用的是 adobe 的 JPGEncoder+ AIR的JPEGEncoderOptions (注 ...
- PL/SQL 听课笔记
PL/SQL: 知识回顾: SQL: 结构化查询语言: T-SQL: microsoft sql语言: PL/SQL: Oracle sql语言: 变量命名规则: 1.首字母必须是字母,可以包含字 ...