`
阅读更多

切面类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() (打印出正在被通知的方法的有用信息。

分享到:
评论

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    aop所依赖的所有包

    AOP(Aspect-Oriented Programming,面向切面编程)是一种编程范式,旨在减少代码的重复性和增强可维护性,特别是在处理系统中的横切关注点时。这些关注点,如日志、事务管理、安全检查等,往往分散在系统的各个部分...

    aopalliance-1.0.jar及aopalliance源码

    **AOP Alliance简介** AOP Alliance是一个开源项目,它的全称是Aspect Oriented Programming(面向切面编程)Alliance,是Java平台上的一个接口集合,为面向切面编程的实现提供了一个统一的API。这个库的主要目的是...

    反射实现 AOP 动态代理模式(Spring AOP 的实现原理)

    面向切面编程(AOP)是一种编程范式,旨在将横切关注点(如日志、安全等)与业务逻辑分离,从而提高模块化。AOP通过预定义的“切面”对横切关注点进行模块化,从而可以在不修改业务逻辑代码的情况下增加新功能。动态...

    spring aop jar 包

    Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。这个"spring aop jar 包"包含了实现这一功能所需的类和接口,...

    aopalliance最新完整jar包

    在Java应用中,aopalliance.jar包扮演着至关重要的角色,它包含了一些核心接口,如`org.aopalliance.intercept.MethodInterceptor`和`org.aopalliance.aop.Advice`,这些接口定义了拦截器和通知的概念,它们是AOP的...

    spring-boot aop

    Spring Boot AOP(面向切面编程)是一种强大的设计模式,它允许我们在不修改现有代码的情况下,插入额外的功能或监控代码。在Spring框架中,AOP主要用于日志记录、事务管理、性能统计等场景。本示例是关于如何在...

    springAOP所依赖的jar包

    Spring AOP(面向切面编程)是Spring框架的重要组成部分,它允许程序员定义“切面”,这些切面可以封装跨越多个对象的行为或责任。在Java应用中实现AOP通常需要依赖于一些外部库,这些库在你提供的标题和描述中有所...

    利用C#实现AOP常见的几种方法详解

    面向切面编程(AOP,Aspect Oriented Programming)是一种编程范式,旨在通过将关注点分离,使得系统设计更加模块化。AOP的核心思想是将应用程序的横切关注点(如日志、事务管理、安全检查等)从核心业务逻辑中解耦...

    aop开发环境jar包

    在IT行业中,AOP(Aspect-Oriented Programming,面向切面编程)是一种编程范式,它旨在提高软件的模块化程度,将关注点分离。在Java世界里,AOP常用于处理日志、事务管理、权限检查等横切关注点。当我们谈到“AOP...

    spring-aop-jar

    在IT领域,Spring框架是一个广泛使用的Java应用框架,它提供了许多功能,包括依赖注入、面向切面编程(AOP)等。"spring-aop-jar"这个主题涉及到Spring框架中的核心组件之一——Spring AOP。这里我们将深入探讨...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring aop spring aop

    在给出的XML配置中,`&lt;aop:config&gt;`元素开启AOP支持,而`&lt;aop:aspect&gt;`元素用于定义切面,其内部通过`&lt;aop:pointcut&gt;`定义切点,并通过`&lt;aop:before&gt;`和`&lt;aop:after&gt;`指定通知。 为了使用这些配置,我们需要在代码...

    C#版本AOP注入

    在IT行业中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种设计模式,它旨在提高软件的模块化程度,将关注点分离,使业务逻辑与系统服务(如日志、事务管理、安全控制等)解耦。C#作为.NET框架的主要...

    springAop与spring定时器

    Spring AOP(面向切面编程)是Spring框架中的一个重要组件,它允许我们在不修改源代码的情况下,通过在程序运行时动态地将代码插入到方法调用中,来实现跨切面的关注点,如日志记录、性能监控、事务管理等。...

    C# .net Aop 动态截获异常

    在.NET开发环境中,C#语言提供了丰富的特性(Attributes)、依赖注入(DI)和面向切面编程(AOP)等机制,使得我们可以构建更加灵活、可维护的代码。本主题将深入探讨如何使用C#和AOP来动态截获异常,以实现更高级别...

    Spring使用AOP的三个jar包

    最后,`aopalliance-1.0.0.jar`是AOP联盟提供的一个接口库,它定义了一些通用的AOP接口,比如`org.aopalliance.intercept.MethodInterceptor`和`org.aopalliance.intercept.MethodInvocation`,使得不同的AOP框架...

    aop@work.rar

    《面向切面编程(AOP)的工作原理与实践》 面向切面编程(Aspect-Oriented Programming,简称AOP)是软件开发中的一个重要概念,它旨在解决程序中的横切关注点,即那些跨越多个模块、类或方法的共同功能,如日志、...

    aopalliance-1.0.jar下载

    《aopalliance-1.0.jar:AOP联盟的核心库解析》 在Java开发领域,面向切面编程(Aspect-Oriented Programming, AOP)是一种重要的编程范式,它旨在将关注点分离,使系统设计更为模块化,降低耦合度。而aopalliance-...

    Spring中的AOP不生效

    ### Spring中的AOP不生效的原因及解决方法 在Java开发中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种重要的编程思想和技术手段,主要用于处理横切关注点问题,如日志记录、性能统计、安全控制、...

Global site tag (gtag.js) - Google Analytics