aop注解

来源:互联网 发布:数据库工程师考试 编辑:程序博客网 时间:2024/05/22 21:18
.AOP注解配置


(1)在spring配置文件中启用AOP注解
   <aop:aspectj-autoproxy/>
 (2)编写切面组件,使用以下注解定义切入点,通知等
 @Aspect : 将当前类指定为切面组件
 @Pointcut : 定义切入点
 @Around("切入点引用")
 @Before("切入点引用")
 @After("切入点引用")
 @AfterReturning(pointcut="切入点",returning="参数名")
 @AfterThrowing(pointcut="切入点",throwing="参数名")
2.切入点定义


(1) execution表达式 : 匹配方法的连接点.
execution (modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)
      throws-pattern?) 
(2) bean表达式 : 匹配容器中bean对象的id或name属性值
  bean (idOrNameOfBean)
(3) within表达式: 匹配某一个或某一批特定类型
  within(包名.类型)
示例1:在com.xyz.service.DeptService接口中的任意连接点 
within(com.xyz.service.DeptService) 
示例2:在service包中的任意连接点 
within(com.xyz.service.*) 
示例3:在service包或其子包中的任意连接点 
within(com.xyz.service..*) 
(4) this和target : 匹配某一个类型实例的任意连接点.与within区别在于不允许使用通配符 
this(代理对象类型),target(目标对象类型) 
示例1 : 实现了AccountService接口的代理对象的任意连接点 
this (com.xyz.service.AccountService) 
示例2 : 实现AccountService接口的目标对象的任意连接点 
target (com.xyz.service.AccountService) 
(5) args表达式 : 匹配方法参数类型的连接点 
args (参数类型) 
示例1 : 任何一个只接受一个参数, 
并且运行时所传入的参数是Serializable 接口的连接点 
args (java.io.Serializable) 
(6)以上表达式可以使用运算符组合 
||,&&,!,and,or,not
0 0