基于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();}}

运行程序,控制台输出: