Spring Aspectj AOP配置学习

Spring AOP 用户可能会经常使用 execution pointcut designator。执行表达式的格式如下:
     execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)   execution(修饰匹配(可选) 返回类型(必填) 声明类型(可选) 方法名称(参数匹配) 抛出异常类型(可选))

除了返回类型模式(上面代码片断中的ret-type-pattern),名字模式和参数模式以外,所有的部分都是可选的。 返回类型模式决定了方法的返回类型必须依次匹配一个连接点。 你会使用的最频繁的返回类型模式是 *,它代表了匹配任意的返回类型。 一个全称限定的类型名将只会匹配返回给定类型的方法。名字模式匹配的是方法名。 你可以使用 * 通配符作为所有或者部分命名模式。 参数模式稍微有点复杂:() 匹配了一个不接受任何参数的方法, 而 (..) 匹配了一个接受任意数量参数的方法(零或者更多)。 模式 (*) 匹配了一个接受一个任何类型的参数的方法。 模式 (*,String) 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型。

 

对于execution()可以使用&& || !等操作符将各个表达式组合起来,限制切入点

 

  下面给出一些常见切入点表达式的例子。   

  任意公共方法的执行:  
  
  execution(public * *(..))任何一个以“set”开始的方法的执行:  
  
  execution(* set*(..))AccountService 接口的任意方法的执行:  
  
  execution(* com.xyz.service.AccountServ定ice.*(..))定义在service包里的任意方法的执行:  
  
  execution(* com.xyz.service.*.*(..))定义在service包或者子包里的任意方法的执行:  
  
  execution(* com.xyz.service..*.*(..))在service包里的任意连接点(在Spring AOP中只是方法执行) :  
  
  within(com.xyz.service.*)在service包或者子包里的任意连接点(在Spring AOP中只是方法执行) :  
  
  within(com.xyz.service..*)实现了 AccountService 接口的代理对象的任意连接点(在Spring AOP中只是方法执行) :  
  
  this(com.xyz.service.AccountService)'this'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得代理对象可以在通知体内访问到的部分。   
实现了 AccountService 接口的目标对象的任意连接点(在Spring AOP中只是方法执行) :  
  
  target(com.xyz.service.AccountService)'target'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得目标对象可以在通知体内访问到的部分。   
任何一个只接受一个参数,且在运行时传入的参数实现了 Serializable 接口的连接点 (在Spring AOP中只是方法执行)   
  
  args(java.io.Serializable)'args'在binding form中用的更多:- 请常见以下讨论通知的章节中关于如何使得方法参数可以在通知体内访问到的部分。   
   请注意在例子中给出的切入点不同于 execution(* *(java.io.Serializable)): args只有在动态运行时候传入参数是可序列化的(Serializable)才匹配,而execution 在传入参数的签名声明的类型实现了 Serializable 接口时候匹配。  
  
  有一个 @Transactional 注解的目标对象中的任意连接点(在Spring AOP中只是方法执行)   
  
  @target(org.springframework.transaction.annotation.Transactional)'@target' 也可以在binding form中使用:请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。   
任何一个目标对象声明的类型有一个 @Transactional 注解的连接点(在Spring AOP中只是方法执行)  
  
  @within(org.springframework.transaction.annotation.Transactional)'@within'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。   
任何一个执行的方法有一个 @Transactional annotation的连接点(在Spring AOP中只是方法执行)

  @annotation(org.springframework.transaction.annotation.Transactional)'@annotation' 也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。   
任何一个接受一个参数,并且传入的参数在运行时的类型实现了 @Classified annotation的连接点(在Spring AOP中只是方法执行)   
  
  @args(com.xyz.security.Classified)'@args'也可以在binding form中使用:- 请常见以下讨论通知的章节中关于如何使得annotation对象可以在通知体内访问到的部分。

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

  一。使用零配置格式 spring 关于aop的配置文件格式

[xhtml] view plain copy
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.            http://www.springframework.org/schema/tx  
  12.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.     <aop:aspectj-autoproxy proxy-target-class="true" />  
  14.         <context:component-scan base-package="com">  
  15.         <context:include-filter type="annotation"   
  16.             expression="org.aspectj.lang.annotation.Aspect"/>  
  17.     </context:component-scan>    
  18.     <bean  class="用户定义的切面类"></bean>  
  19. </beans>  

<aop:aspectj-autoproxy proxy-target-class="true" /> 要加上黑体字,如果不加上的话,会可能出现编译通不过去

指定自动搜索bean组件,自动搜索切面类

<context:component-scan base-package="com">
        <context:include-filter type="annotation"
            expression="org.aspectj.lang.annotation.Aspect"/>
    </context:component-scan>

但是以上两个配置最好不要同时出现,否则导致执行两次切面类

java类

定义切入点

  1. @Aspect  
  2. public class CommonPointcutsDefinition {  
  3.     @Pointcut("execution(public * com.x.x.x.x.x.UserLogin(..))")  
  4.     public void loginServer(){}  
  5.       
  6.     @Pointcut("execution(public * com.x.x.x.x.x.execute(..))")  
  7.     public void logoutServer(){}  
  8. }  

定义advice类

  1. @Aspect  
  2. public class CommonAdvice {  
  3.       
  4.     @Before("com.x.x.x.CommonPointcutsDefinition.loginServer()")  
  5.     public void loginBeforeInfo(){  
  6.         System.out.println(currentDate + ipAddress + "试图登录系统");  
  7.     }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值