Spring AOP 详解

此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题。最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP来解决。一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习SpringAOP相关的内容。本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智。

  1. 对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况
  2. 监控部分重要函数,若抛出指定的异常,需要以短信或邮件方式通知相关人员
  3. 金控部分重要函数的执行时间

    事实上,以上需求没有AOP也能搞定,只是在实现过程中比较郁闷摆了。

  1. 需要打印日志的函数分散在各个包中,只能找到所有的函数体,手动添加日志。然而这些日志都是临时的,待问题解决之后应该需要清除打印日志的代码,只能再次手动清除^_^!
  2. 类似1的情况,需要捕获异常的地方太多,如果手动添加时想到很可能明天又要手动清除,只能再汗。OK,该需求相对比较固定,属于长期监控的范畴,并不需求临时添加后再清除。然而,客户某天要求,把其中20%的异常改为短信提醒,剩下的80%改用邮件提醒。改之,两天后,客户抱怨短信太多,全部改成邮件提醒...
  3. 该需求通常用于监控某些函数的执行时间,用以判断系统执行慢的瓶颈所在。瓶颈被解决之后,烦恼同情况1


    终于下定决心,采用AOP来解决!代码如下:

 

    切面类TestAspect

Java代码   收藏代码
  1. package com.spring.aop;  
  2. /** 
  3.  * 切面 
  4.  * 
  5.  */  
  6. public class TestAspect {  
  7.   
  8.     public void doAfter(JoinPoint jp) {  
  9.         System.out.println("log Ending method: "  
  10.                 + jp.getTarget().getClass().getName() + "."  
  11.                 + jp.getSignature().getName());  
  12.     }  
  13.   
  14.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  15.         long time = System.currentTimeMillis();  
  16.         Object retVal = pjp.proceed();  
  17.         time = System.currentTimeMillis() - time;  
  18.         System.out.println("process time: " + time + " ms");  
  19.         return retVal;  
  20.     }  
  21.   
  22.     public void doBefore(JoinPoint jp) {  
  23.         System.out.println("log Begining method: "  
  24.                 + jp.getTarget().getClass().getName() + "."  
  25.                 + jp.getSignature().getName());  
  26.     }  
  27.   
  28.     public void doThrowing(JoinPoint jp, Throwable ex) {  
  29.         System.out.println("method " + jp.getTarget().getClass().getName()  
  30.                 + "." + jp.getSignature().getName() + " throw exception");  
  31.         System.out.println(ex.getMessage());  
  32.     }  
  33.   
  34.     private void sendEx(String ex) {  
  35.         //TODO 发送短信或邮件提醒  
  36.     }  
  37. }   

 

 

Java代码   收藏代码
  1. package com.spring.service;  
  2. /** 
  3.  * 接口A 
  4.  */  
  5. public interface AService {  
  6.       
  7.     public void fooA(String _msg);  
  8.   
  9.     public void barA();  
  10. }  
 
Java代码   收藏代码
  1. package com.spring.service;  
  2. /** 
  3.  *接口A的实现类 
  4.  */  
  5. public class AServiceImpl implements AService {  
  6.   
  7.     public void barA() {  
  8.         System.out.println("AServiceImpl.barA()");  
  9.     }  
  10.   
  11.     public void fooA(String _msg) {  
  12.         System.out.println("AServiceImpl.fooA(msg:"+_msg+")");  
  13.     }  
  14. }  

 

 

Java代码   收藏代码
  1. package com.spring.service;  
  2.   
  3. /** 
  4.  *   Service类B 
  5.  */  
  6. public class BServiceImpl {  
  7.   
  8.     public void barB(String _msg, int _type) {  
  9.         System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");  
  10.         if(_type == 1)  
  11.             throw new IllegalArgumentException("测试异常");  
  12.     }  
  13.   
  14.     public void fooB() {  
  15.         System.out.println("BServiceImpl.fooB()");  
  16.     }  
  17.   
  18. }  
 

    ApplicationContext

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  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.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans  
  7.             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.             http://www.springframework.org/schema/aop  
  9.             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  
  10.     default-autowire="autodetect">  
  11.     <aop:config>  
  12.         <aop:aspect id="TestAspect" ref="aspectBean">  
  13.             <!--配置com.spring.service包下所有类或接口的所有方法-->  
  14.             <aop:pointcut id="businessService"  
  15.                 expression="execution(* com.spring.service.*.*(..))" />  
  16.             <aop:before pointcut-ref="businessService" method="doBefore"/>  
  17.             <aop:after pointcut-ref="businessService" method="doAfter"/>  
  18.             <aop:around pointcut-ref="businessService" method="doAround"/>  
  19.             <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
  20.         </aop:aspect>  
  21.     </aop:config>  
  22.       
  23.     <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
  24.     <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
  25.     <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
  26.   
  27. </beans>  

 

    测试类AOPTest

Java代码   收藏代码
  1. public class AOPTest extends AbstractDependencyInjectionSpringContextTests {  
  2.       
  3.     private AService aService;  
  4.       
  5.     private BServiceImpl bService;  
  6.       
  7.     protected String[] getConfigLocations() {  
  8.         String[] configs = new String[] { "/applicationContext.xml"};  
  9.         return configs;  
  10.     }  
  11.       
  12.       
  13.     /** 
  14.      * 测试正常调用 
  15.      */  
  16.     public void testCall()  
  17.     {  
  18.         System.out.println("SpringTest JUnit test");  
  19.         aService.fooA("JUnit test fooA");  
  20.         aService.barA();  
  21.         bService.fooB();  
  22.         bService.barB("JUnit test barB",0);  
  23.     }  
  24.       
  25.     /** 
  26.      * 测试After-Throwing 
  27.      */  
  28.     public void testThrow()  
  29.     {  
  30.         try {  
  31.             bService.barB("JUnit call barB",1);  
  32.         } catch (IllegalArgumentException e) {  
  33.               
  34.         }  
  35.     }  
  36.       
  37.     public void setAService(AService service) {  
  38.         aService = service;  
  39.     }  
  40.       
  41.     public void setBService(BServiceImpl service) {  
  42.         bService = service;  
  43.     }  
  44. }  

 

    运行结果如下:

Java代码   收藏代码
  1. log Begining method: com.spring.service.AServiceImpl.fooA  
  2. AServiceImpl.fooA(msg:JUnit test fooA)  
  3. log Ending method: com.spring.service.AServiceImpl.fooA  
  4. process time: 0 ms  
  5. log Begining method: com.spring.service.AServiceImpl.barA  
  6. AServiceImpl.barA()  
  7. log Ending method: com.spring.service.AServiceImpl.barA  
  8. process time: 0 ms  
  9. log Begining method: com.spring.service.BServiceImpl.fooB  
  10. BServiceImpl.fooB()  
  11. log Ending method: com.spring.service.BServiceImpl.fooB  
  12. process time: 0 ms  
  13. log Begining method: com.spring.service.BServiceImpl.barB  
  14. BServiceImpl.barB(msg:JUnit test barB type:0)  
  15. log Ending method: com.spring.service.BServiceImpl.barB  
  16. process time: 0 ms  
  17.   
  18. log Begining method: com.spring.service.BServiceImpl.barB  
  19. BServiceImpl.barB(msg:JUnit call barB type:1)  
  20. log Ending method: com.spring.service.BServiceImpl.barB  
  21. method com.spring.service.BServiceImpl.barB throw exception  
  22. 测试异常  
 

    《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(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方法。

       切入点表达式

  • 通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下:
Java代码   收藏代码
  1. execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)  

modifiers-pattern:方法的操作权限

ret-type-pattern:返回值

declaring-type-pattern:方法所在的包

name-pattern:方法名

parm-pattern:参数名

throws-pattern:异常

其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。

  • 通知参数

可以通过args来绑定参数,这样就可以在通知(Advice)中访问具体参数了。例如,<aop:aspect>配置如下

Java代码   收藏代码
  1. <aop:config>  
  2.     <aop:aspect id="TestAspect" ref="aspectBean">  
  3.         <aop:pointcut id="businessService"  
  4.             expression="execution(* com.spring.service.*.*(String,..)) and args(msg,..)" />  
  5.             <aop:after pointcut-ref="businessService" method="doAfter"/>  
  6.     </aop:aspect>  
  7. </aop:config>  

TestAspect的doAfter方法中就可以访问msg参数,但这样以来AService中的barA()和BServiceImpl中的barB()就不再是连接点,因为execution(* com.spring.service.*.*(String,..))只配置第一个参数为String类型的方法。其中,doAfter方法定义如下:

Java代码   收藏代码
  1. public void doAfter(JoinPoint jp,String msg)  
  •   访问当前的连接点

任何通知(Advice)方法可以将第一个参数定义为 org.aspectj.lang.JoinPoint 类型。JoinPoint 接口提供了一系列有用的方法, 比如getArgs()(返回方法参数)、getThis()(返回代理对象)、getTarget()(返回目标)、getSignature()(返回正在被通知的方法相关信息)和toString()(打印出正在被通知的方法的有用信息。

 

AOP的理解

1、AOP的概述

AOP是一种不同于OOP(面向对象编程)的编程模式,它不是OOP的替代,而是对OOP的一种有益补充。

2、spring AOP的原理

3、spring AOP的实现

在spring2.5中,常用的AOP实现方式有两种。第一种是基于xml配置文件方式的实现,第二种是基于注解方式的实现。

接下来,以具体的是理智讲解这两种方式的使用。

Java代码

  
  
  1. package com.zxf.service;     
  2.     
  3. /**    
  4.  * 业务逻辑接口    
  5.  * @author z_xiaofei168    
  6.  */    
  7. public interface AccountService {     
  8.     public void save(String loginname, String password);     
  9. }     
  10.     
  11. 它的实现类     
  12.     
  13. package com.zxf.service;     
  14. import com.zxf.dao.AccountDao;     
  15.     
  16. /**    
  17.  * AccountService的实现类    
  18.  * @author z_xiaofei168    
  19.  */    
  20. public class AccountServiceImpl implements AccountService {     
  21.     private  AccountDao accountDao;     
  22.          
  23.     public AccountServiceImpl() {}     
  24.          
  25.     /** 带参数的构造方法 */    
  26.     public AccountServiceImpl(AccountDao accountDao){     
  27.         this.accountDao = accountDao;     
  28.     }     
  29.          
  30.     public void save(String loginname, String password) {     
  31.         accountDao.save(loginname, password);     
  32.         throw new RuntimeException("故意抛出一个异常。。。。");     
  33.     }     
  34.          
  35.     /** set方法 */    
  36.     public void setAccountDao(AccountDao accountDao) {     
  37.         this.accountDao = accountDao;     
  38.     }     
  39. }  

对于业务系统来说,AccountServiceImpl类就是目标实现类,它的业务方法,如save()方法的前后或代码会出现异常的地方都是AOP的连接点。

下面是日志服务类的代码:

Java代码

  
  
  1. package com.zxf.aspect;     
  2.     
  3. import org.aspectj.lang.JoinPoint;     
  4. import org.aspectj.lang.ProceedingJoinPoint;     
  5.     
  6. /**    
  7.  * 日志切面类    
  8.  * @author z_xiaofei168    
  9.  */    
  10. public class LogAspect {     
  11.     
  12.     //任何通知方法都可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型      
  13.     public void before(JoinPoint call) {     
  14.         //获取目标对象对应的类名     
  15.         String className = call.getTarget().getClass().getName();     
  16.         //获取目标对象上正在执行的方法名     
  17.         String methodName = call.getSignature().getName();     
  18.              
  19.         System.out.println("前置通知:" + className + "类的" + methodName + "方法开始了");     
  20.     }     
  21.          
  22.     public void afterReturn() {     
  23.         System.out.println("后置通知:方法正常结束了");     
  24.     }     
  25.          
  26.     public void after(){     
  27.         System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回的");     
  28.     }     
  29.          
  30.     public void afterThrowing() {     
  31.         System.out.println("异常抛出后通知:方法执行时出异常了");     
  32.     }     
  33.          
  34.     //用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型     
  35.     public Object doAround(ProceedingJoinPoint call) throws Throwable {     
  36.         Object result = null;     
  37.         this.before(call);//相当于前置通知     
  38.         try {     
  39.             result = call.proceed();     
  40.             this.afterReturn(); //相当于后置通知     
  41.         } catch (Throwable e) {     
  42.     
  43.             this.afterThrowing();  //相当于异常抛出后通知     
  44.             throw e;     
  45.         }finally{     
  46.             this.after();  //相当于最终通知     
  47.         }     
  48.              
  49.         return result;     
  50.     }     
  51. }   

这个类属于业务服务类,如果用AOP的术语来说,它就是一个切面类,它定义了许多通知。Before()、afterReturn()、after()和afterThrowing()这些方法都是通知。

<1>.基于xml配置文件的AOP实现

这种方式在实现AOP时,有4个步骤。

Xml代码

  
  
  1. xml version="1.0" encoding="UTF-8"?>    
  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.         xsi:schemaLocation="     
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>    
  8.     
  9.     <bean id="accountDaoImpl" class="com.zxf.dao.AccountDaoImpl"/>    
  10.          
  11.     <bean id="accountService" class="com.zxf.service.AccountServiceImpl">    
  12.         <property name=" accountDaoImpl " ref=" accountDaoImpl "/>    
  13.     bean>    
  14.     
  15.         
  16.     <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>    
  17.          
  18.         
  19.     <aop:config>    
  20.             
  21.         <aop:aspect id="logAspect" ref="logAspectBean">    
  22.                 
  23.             <aop:pointcut id="allMethod"      
  24.                 expression="execution(* com.zxf.service.*.*(..))"/>    
  25.                      
  26.                 
  27.             <aop:before method="before" pointcut-ref="allMethod" />    
  28.                 
  29.             <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>    
  30.                 
  31.             <aop:after method="after" pointcut-ref="allMethod"/>    
  32.                 
  33.             <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>    
  34.                  
  35.                 
  36.                 
  37.         aop:aspect>    
  38.     aop:config>    
  39. beans>    

上述配置针对切入点应用了前置、后置、最终,以及抛出异常后通知。这样在测试执行AccountServiceImpl类的save()方法时,控制台会有如下结果输出。

前置通知:com.zxf.service.AccountServiceImpl类的save方法开始了。

针对MySQL的AccountDao实现中的save()方法。

后置通知:方法正常结束了。

最终通知:不管方法有没有正常执行完成,一定会返回的。

<2>基于注解的AOP的实现

首先创建一个用来作为切面的类LogAnnotationAspect,同时把这个类配置在spring的配置文件中。

在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了对AspectJ基于注解的切面的支持,从而 更进一步地简化AOP的配置。具体的步骤有两步。

Spring的配置文件是如下的配置:

Xml代码

  
  
  1. xml version="1.0" encoding="UTF-8"?>    
  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.         xsi:schemaLocation="     
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>    
  8.     
  9.     <bean id="accountDao" class="com.zxf.dao.AccountDaoImpl"/>    
  10.     <bean id="accountService" class="com.zxf.service.AccountServiceImpl">    
  11.         <property name="accountDao" ref="accountDao"/>    
  12.     bean>    
  13.         
  14.     <bean id="logAspectBean" class="com.zxf.aspect.LogAnnotationAspect"/>    
  15.         
  16.     <aop:aspectj-autoproxy/>    
  17. beans>   

这是那个切面的类LogAnnotationAspect

Java代码

  
  
  1. package com.zxf.aspect;     
  2.     
  3. import org.aspectj.lang.JoinPoint;     
  4. import org.aspectj.lang.ProceedingJoinPoint;     
  5. import org.aspectj.lang.annotation.After;     
  6. import org.aspectj.lang.annotation.AfterReturning;     
  7. import org.aspectj.lang.annotation.AfterThrowing;     
  8. import org.aspectj.lang.annotation.Aspect;     
  9. import org.aspectj.lang.annotation.Before;     
  10. import org.aspectj.lang.annotation.Pointcut;     
  11.     
  12. /**    
  13.  * 日志切面类    
  14.  */    
  15. @Aspect  //定义切面类     
  16. public class LogAnnotationAspect {     
  17.     @SuppressWarnings("unused")     
  18.     //定义切入点     
  19.     @Pointcut("execution(* com.zxf.service.*.*(..))")     
  20.     private void allMethod(){}     
  21.          
  22.     //针对指定的切入点表达式选择的切入点应用前置通知     
  23.     @Before("execution(* com. zxf.service.*.*(..))")     
  24.     public void before(JoinPoint call) {     
  25.              
  26.         String className = call.getTarget().getClass().getName();     
  27.         String methodName = call.getSignature().getName();     
  28.              
  29.         System.out.println("【注解-前置通知】:" + className + "类的"      
  30.                 + methodName + "方法开始了");     
  31.     }     
  32.     //访问命名切入点来应用后置通知     
  33.     @AfterReturning("allMethod()")     
  34.     public void afterReturn() {     
  35.         System.out.println("【注解-后置通知】:方法正常结束了");     
  36.     }     
  37.          
  38.     //应用最终通知     
  39.     @After("allMethod()")     
  40.     public void after(){     
  41.         System.out.println("【注解-最终通知】:不管方法有没有正常执行完成,"      
  42.                 + "一定会返回的");     
  43.     }     
  44.          
  45.     //应用异常抛出后通知     
  46.     @AfterThrowing("allMethod()")     
  47.     public void afterThrowing() {     
  48.         System.out.println("【注解-异常抛出后通知】:方法执行时出异常了");     
  49.     }     
  50.          
  51.     //应用周围通知     
  52.     //@Around("allMethod()")     
  53.     public Object doAround(ProceedingJoinPoint call) throws Throwable{     
  54.         Object result = null;     
  55.         this.before(call);//相当于前置通知     
  56.         try {     
  57.             result = call.proceed();     
  58.             this.afterReturn(); //相当于后置通知     
  59.         } catch (Throwable e) {     
  60.             this.afterThrowing();  //相当于异常抛出后通知     
  61.             throw e;     
  62.         }finally{     
  63.             this.after();  //相当于最终通知     
  64.         }     
  65.              
  66.         return result;     
  67.     }     
  68. }    

备注:输出结果和前面的一样。

spring 中的<aop:advisor>和<aop:aspect>有什么区别?不同之处在那里,请大牛们帮小弟解一下疑惑

Java代码   收藏代码
  1. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  2.         <tx:attributes>  
  3.             <tx:method name="save*" propagation="REQUIRED"/>  
  4.             <tx:method name="delete*" propagation="REQUIRED"/>  
  5.             <tx:method name="get*" read-only="true"/>  
  6.             <tx:method name="find*" read-only="true"/>  
  7.         </tx:attributes>  
  8.     </tx:advice>  
  9.     <bean id="dataAccessThrowsAdvice" class="com.XXX.XXX.aop.exception.XXX" />  
  10.     <bean id="serviceThrowsAdvice" class="com.XXXXXX.aop.exception.XXX" />  
  11.     <bean id="actionThrowsAdvice" class="com.XXX.XXXaop.exception.XXX" />  
  12.      <aop:aspectj-autoproxy/>  
  13.     <aop:config proxy-target-class="true">  
  14.         <!-- 切面事务归txAdvice处理 -->  
  15.         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.XXX..*.*Dao.*(..))" />  
  16.         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.XXX..*.*Manager.*(..))" />  
  17.         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.XXX..*.*Action.*(..))" />  
  18.         <aop:advisor advice-ref="dataAccessThrowsAdvice" pointcut="execution(* com.XXX..*.*Dao.*(..))"/>  
  19.         <aop:advisor advice-ref="serviceThrowsAdvice" pointcut="execution(* com.XXX..*.*Dao.*(..))" />  
  20.         <aop:advisor advice-ref="actionThrowsAdvice" pointcut="execution(* com.XXX..*.*Action.*(..))" />  
  21.     </aop:config>  

 spring 中的<aop:advisor>和<aop:aspect>有什么区别?不同之处在那里,请大牛们帮小弟解一下疑惑配置文件如上

 

然后我写了一个类:如下:

Java代码   收藏代码
  1. import org.springframework.aop.MethodBeforeAdvice;  
  2. import org.springframework.aop.ThrowsAdvice;  
  3.   
  4. public class BaseBeforeAdvice implements MethodBeforeAdvice{  
  5.   
  6.     @Override  
  7.     public void before(Method arg0, Object[] arg1, Object arg2)  
  8.             throws Throwable {  
  9.         System.out.println("******************");  
  10.         System.out.println(arg0.getName());  
  11.         System.out.println("******************");  
  12.           
  13.           
  14.     }  

 小弟不明白这个方法和这种有什么区别:

Java代码   收藏代码
  1. @Aspect  
  2. public class wqAdvice {  
  3.    @Pointcut("execution (* com.XX.*.XX.*(..))")  
  4.    private void myXXCut(){}  
  5.    @Before("myXXCut()")  
  6.    public void doBefore(){  
  7.        System.out.println("before");  
  8.    }  
  9.    @AfterReturning(pointcut="myXXCut()",returning="result")  
  10.    public void doAfter(String result){  
  11.        System.out.println("atfer"+result);  
  12.    }  
  13.    @AfterThrowing(pointcut=" myXXCut()",throwing="e")  
  14.    public void doThrowing(Exception e){  
  15.        System.out.println("Exception"+e.getMessage());  
  16.    }  
  17. }  

 疑惑颇重,希望大牛们指点小弟一下




































































1、Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 
2、区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcut和多个advice

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值