Spring中的aspectj注解AOP
Spring中的aspectj注解AOP
常用的有
AfterAfterReturningAfterThrowingAroundAspectBefore
注解的执行
没有异常的执行顺序
1 | around begin -> before -> afterReturning -> after -> around end |
抛出异常的执行顺序
1 | around begin -> before -> AfterThrowing -> after |
表达式Pointcut
主要语法
指示器(designators )
匹配方法
execution:
匹配参数
args
匹配对象
thisbeantarget(类)
匹配包/类型
within(类)
匹配注解
@args@target(类)@within(类)@annotation
通配符(wildcards)
*匹配任意字符..匹配指定类及其子类+匹配任意数的子包或参数
运算符(operators)
&&与||或!非
表达式语法
execution
1 | execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?) |
其中 三个必须的:
returning type patternname patternparameters pattern
示例
execution(* com.*.*(..))匹配com包里类的任意方法execution(* com..*.*(..))匹配com包及其所有子包里类的任意方法execution(* com..Demo*.*(..))匹配com包及其所有子包里以Demo开头类的任意方法
within
within(@org.springframework.web.bind.annotation.RestController *)匹配注解下任意类的任意方法within(com.)匹配com包里的任意类within(com..)匹配com包及其所有子包里的任意类
this
this(com.IService)匹配IService接口的所有实现类
@within
@within(org.springframework.web.bind.annotation.RestController)匹配注解下任意类的任意方法
@target
@target(org.springframework.web.bind.annotation.RestController)匹配注解下任意类的任意方法
@annotation
@annotation(org.springframework.transaction.annotation.Transactional)匹配注解下的方法
常用示例
扫描注解标注的方法
1 | public class AspectjAop{ |
扫描注解标注的类
1 | public class AspectjAop{ |
多个条件
1 | public class AspectjAop{ |
附
全部表达式
1 | new InternalUseOnlyPointcutParser(ClassLocader) |
自定义 AOP 表达式
1 | public class Demo( |