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的更多相关文章

  1. spring 理解Spring AOP 一个简单的约定游戏

    应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...

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

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

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

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  4. Spring AOP中的动态代理

    0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  Spring AOP中的动态代理机制 2.1  ...

  5. 轻松理解 Spring AOP

    目录 Spring AOP 简介 Spring AOP 的基本概念 面向切面编程 AOP 的目的 AOP 术语和流程 术语 流程 五大通知执行顺序 例子 图例 实际的代码 使用 Spring AOP ...

  6. Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)

    本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...

  7. spring aop中pointcut表达式完整版

    spring aop中pointcut表达式完整版 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @with ...

  8. Spring AOP中的JDK和CGLib动态代理哪个效率更高?

    一.背景 今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib动态代理哪个效率更高? 二.基本概念 首先,我们知道Spring AOP的底层实现有两种方式:一种是JDK动态代理, ...

  9. 深入理解Spring AOP之二代理对象生成

    深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...

随机推荐

  1. Linux运维命令之一

    释放内存:syncecho 3 > /proc/sys/vm/drop_caches Linux查看Dell服务器型号命令:dmidecode | grep "Product Name ...

  2. 【leetcode❤python】 299. Bulls and Cows

    #-*- coding: UTF-8 -*-class Solution(object):      def getHint(self, secret, guess):          " ...

  3. FreeSWITCH 1.6在Debian 8上的安装

    鉴于上次在CentOS 7上安装不成功,这次换Debian. 现在已经成功的CentOS 7上安装好了. 感兴趣的同学移步https://freeswitch.org/confluence/displ ...

  4. Java——Socket编程(一)

    1. 网络基础知识 两台机器之间需要进行通信,需要满足的条件: 每个机器有一个唯一的标识符(IP地址): 他们之间进行通信需要用同一种语言(协议): 每台主机上面有多个应用程序,如QQ,微博,迅雷等, ...

  5. [HDU5907]Find Q(水)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5907 记下每块大小,然后n*(n+1)/2 #include <bits/stdc++.h> ...

  6. CentOS 6.5 64位 安装zabbix-2.2.0

    安装环境: VM 10 + CentOS-6.5-x86_64-minimal 虚拟机网络是NAT方式, 动态IP Xshell登录到Centos操作 刚装的centos,啥都没有,先配一下yum 首 ...

  7. poj 1696 Space Ant (极角排序)

    链接:http://poj.org/problem?id=1696 Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  8. CSS笔记(五)字体

    CSS 字体属性定义文本的字体系列.大小.加粗.风格(如斜体)和变形(如小型大写字母). 参考:http://www.w3school.com.cn/css/css_font.asp CSS字体系列 ...

  9. css样式 浏览器的读取顺序

    css样式 浏览器的读取顺序 例: tbody tr td{} 浏览器是先查找td,然后去找td tr,然后去找td tr tbody div p{}和div>p{}的区别 .div p{} 是 ...

  10. iOS - OC Copy 拷贝

    前言 copy:需要先实现 NSCopying 协议,创建的是不可变副本. mutableCopy:需要实现 NSMutableCopying 协议,创建的是可变副本. 浅拷贝:指针拷贝,源对象和副本 ...