Spring AOP中pointcut expression表达式解析

来源:互联网 发布:照片三维重建软件 编辑:程序博客网 时间:2024/05/05 06:42

Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中execution 是用的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
举例说明:
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))

pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.
this(com.test.spring.aop.pointcutexp.Intf)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.

带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解

参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)
Pointcut 可以通过Java注解和XML两种方式配置,如下所示:

  1. <aop:config>  
  2.     <aop:aspectrefaop:aspectref="aspectDef">  
  3.         <aop:pointcutidaop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/>  
  4.         <aop:before pointcut-ref="pointcut1" method="beforeAdvice" />  
  5.     </aop:aspect>  
  6. </aop:config>  
  7.   
  8. @Component  
  9. @Aspect  
  10. public class AspectDef {  
  11.     //@Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")  
  12.     //@Pointcut("within(com.test.spring.aop.pointcutexp..*)")  
  13.     //@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")  
  14.     //@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")  
  15.     //@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")  
  16.     //@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")  
  17.     @Pointcut("args(String)")  
  18.     public void pointcut1() {  
  19.     }  
  20.     @Before(value = "pointcut1()")  
  21.     public void beforeAdvice() {  
  22.         System.out.println("pointcut1 @Before...");  
  23.     }  

=================================================

@Aspect 示例 及XML方式的xml文件的配置

  1. import org.aspectj.lang.annotation.*;  
  2. import org.springframework.stereotype.Component;  
  3.   
  4. @Aspect  
  5. @Component  
  6. public class LogInterceptor {  
  7.     @Pointcut("execution(public * com.userService.*.add(..))")  // 统配方式  
  8.     public void mytime(){}  //当多个同时使用同样的  
  9.       
  10.     long startTime,endTime;  
  11.     @Before("mytime()")  
  12.     public void before(){  
  13.         startTime=System.nanoTime();  
  14.     }  
  15.     @After("mytime()")  
  16.     public void after(){  
  17.         endTime=System.nanoTime();  
  18.         System.out.println("程序运行时间: "+(endTime-startTime)+"纳秒");  
  19.     }  
  20. }  



使用XML 配置Aspect 可省略类中的注解

[html] view plaincopyprint?
  1.  <!-- 声明类 ,此类作为切面类使用  -->  
  2.  <bean id="logInterceptor" class="com.aop.LogInterceptor" />  
  3.  <aop:config>  
  4.   <!-- 设置切面名,及切面类 -->  
  5.   <aop:aspect id="logAspect" ref="logInterceptor">  
  6.     
  7.    <!-- 运行前方法配置,先择要执行的方法 ,并设置切入点  -->  
  8.    <aop:before method="before" pointcut="execution(public * com.userService.*.add(..))" />  
  9.      
  10.    <!-- 先设置切入点,待使用  -->  
  11.    <aop:pointcut id="servicePointcut" expression="execution(public * com.userService.*.add(..))" />  
  12.      
  13.    <!-- 运行后方法配置,先择要执行的方法,参考预先设置好的切入点  -->  
  14.    <aop:after method="after" pointcut-ref="servicePointcut" />  
  15.      
  16.   </aop:aspect>  
  17.  </aop:config>  

 

切面类 如下: 

[java] view plaincopyprint?
  1. package com.aop;  
  2.   
  3. public class LogInterceptor {  
  4.           
  5.     long startTime,endTime;  
  6.     public void before(){  
  7.         startTime=System.nanoTime();  
  8.     }  
  9.     public void after(){  
  10.         endTime=System.nanoTime();  
  11.         System.out.println("程序运行时间: "+(endTime-startTime)+"纳秒");  
  12.     }  
  13. }  




0 0