spring aop笔记(使用demo)

转载自:http://blog.csdn.net/mutouyihao/article/details/6655996

定义

  • 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config> 的 proxy-target-class 属性设为true

       通知(Advice)类型

  • 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
  • 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
  • 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
  • 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。

配置文件:

[html]  view plain copy
  1. <?xml version="1.0"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xmlns:tx="http://www.springframework.org/schema/tx"  
  6.         xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.           http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.    
  10.     <bean id="aopHandler1" class="as.huateng.bo.test.AopHandler1"/>  
  11.     <aop:config>  
  12.        <aop:pointcut id="poicut"expression="execution(* as.huateng.bo.impl.test.*.*(..))" />  
  13.        <aop:aspect ref="aopHandler1">  
  14.            <aop:before method="before"pointcut-ref="poicut" />  
  15.            <aop:after method="after"pointcut-ref="poicut" />  
  16.            <aop:around method="around"pointcut-ref="poicut" />  
  17.            <aop:after-throwing method="doThrowing" pointcut-ref="poicut"/>  
  18.            <aop:after-returning method="doReturning" pointcut-ref="poicut"/>  
  19.        </aop:aspect>  
  20.     </aop:config>  
  21. </beans>  


切面类:

[java]  view plain copy
  1. public class AopHandler1  {  
  2.     public void before(JoinPoint joinPoint) {  
  3.        // 获取方法名  
  4.        System.out.println("-----方法:" + joinPoint.getSignature().getName());  
  5.        // 获取参数信息  
  6.        Object[] args = joinPoint.getArgs();  
  7.        for (int i = 0; i < args.length; i++) {  
  8.            System.out.println("-------参数" + args[i]);  
  9.        }  
  10.    
  11.     }  
  12.    
  13.     public Object around(ProceedingJoinPoint p) throws Throwable{  
  14.        System.out.println("-----------comeinto around");  
  15.        Object retVal = p.proceed();    
  16.    
  17.        System.out.println("-----------leave around");  
  18.        return retVal;  
  19.     }  
  20.    
  21.     public void after(JoinPoint joinPoint) {  
  22.        System.out.println("-----------inafter");  
  23.     }  
  24.      
  25.     public void doThrowing(JoinPoint jp) {   
  26.         System.out.println("--------in exception");  
  27.     }   
  28.     public void doReturning(JoinPoint jp) {   
  29.         System.out.println("--------in return");  
  30.     }  
  31. }  


执行顺序

当有异常抛出时,执行顺序为:

Before—around—after—exception

无异常的执行顺序:

Before—around—after—around---return

 

PS:当切面中的方法定义错误时,尤其around方法,就会产生一些其他方法调用不生效的莫名其妙错误

参考链接:

http://www.iteye.com/topic/336873


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值