基于Annotation的Spring AOP: @Around
来源:互联网 发布:淘宝店铺微淘怎么开通 编辑:程序博客网 时间:2024/06/07 08:59
@Around 增强处理是功能比较强大的增强处理,它近似等于Before 和 AfterReturning的总和。@Around既可在执行目标方法之前织入增强动作,也可在执行目标方法之后织入增强动作。@Around甚至可以决定目标方法在什么时候执行,如何执行,更甚者可以完全阻止目标方法的执行。
@Around可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值。
@Around功能虽然强大,但通常需要在线程安全的环境下使用。因此,如果使用普通的Before、AfterReturning就能解决的问题,就没有必要使用Around了。如果需要目标方法执行之前和之后共享某种状态数据,则应该考虑使用Around。尤其是需要使用增强处理阻止目标的执行,或需要改变目标方法的返回值时,则只能使用Around增强处理了。
当定义一个Around增强处理方法时,该方法的第一个形参必须是 ProceedingJoinPoint 类型,在增强处理方法体内,调用ProceedingJoinPoint的proceed方法才会执行目标方法------这就是@Around增强处理可以完全控制目标方法执行时机、如何执行的关键;如果程序没有调用ProceedingJoinPoint的proceed方法,则目标方法不会执行。
调用ProceedingJoinPoint的proceed方法时,还可以传入一个Object[ ]对象,该数组中的值将被传入目标方法作为实参。如果传入的Object[ ]数组长度与目标方法所需要的参数个数不相等,或者Object[ ]数组元素与目标方法所需参数的类型不匹配,程序就会出现异常。
Person.java :
public interface Person {public String sayHello(String name);public void eat(String food);public void divide();}Chinese.java :
@Componentpublic class Chinese implements Person {@Overridepublic void divide() {int a=5/0;System.out.println("divide执行完成!");}@Overridepublic String sayHello(String name) {System.out.println("sayHello方法被调用...");return name+" Hello,Spring AOP";}@Overridepublic void eat(String food) {System.out.println("我正在吃:"+food);}}AroundAdviceTest.java :
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;@Aspectpublic class AroundAdviceTest {@Around("execution(* com.bean.*.*(..))")public Object processTx(ProceedingJoinPoint jp) throws Throwable{System.out.println("执行目标方法之前,模拟开始事务...");Object rvt=jp.proceed(new String[]{"被改变的参数"});System.out.println("执行目标方法之后,模拟结束事务...");return rvt+"新增的内容";}}bean.xml :
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:component-scan base-package="com.bean"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan> <aop:aspectj-autoproxy/> </beans>Test.java :
public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");Person p=(Person) ctx.getBean("chinese");System.out.println(p.sayHello("张三"));p.eat("西瓜");p.divide();}}
运行程序,控制台输出:
- 基于Annotation的Spring AOP: @Around
- 基于Annotation的Spring AOP: @Before
- 基于Annotation的Spring AOP: @AfterReturning
- 基于Annotation的Spring AOP: @AfterThrowing
- 基于Annotation的Spring AOP: @After
- Spring基于annotation的AOP(AspectJ)
- Spring AOP 的引入(三 基于annotation)
- Spring AOP功能--基于Annotation的方式
- 5.spring的aop之@Around
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- Spring中的AOP—基于Annotation的配置方式
- Spring中AOP基于Annotation的零配置(1)
- Spring中AOP基于Annotation的零配置(2)
- 基于Spring的AOP实现自定义annotation操作日志
- 按键精灵,鼠标自动点击,游戏助手
- .NET中STAThread和MTAThread
- linux下 简单学习epoll处理海量链接
- c# 内存共享、内存映射文件、适合读取大文件
- Linux中编写自己的I2C总线模块
- 基于Annotation的Spring AOP: @Around
- linux open(转载+整理)
- TLB & cahce & 缓存对齐
- [小说]魔王冢(22)启程
- UITableView编辑模式
- 尝试探索基于Linux C的网卡抓包过程
- redis+nodejs实现队列的思路
- windows下的vim编辑器及vim的配置
- ubuntu12.04输入了正确的用户名密码但是无法登录