-
Spring AOP支持在切入点表达式中使用如下的AspectJ切入点指示符
切入点指示符(PCD) | 说明 | 织入方式 |
---|---|---|
execution | 匹配表达式中定义的类型 | 动态织入 |
within | 匹配表达式中定义的包、子包中定义的连接点 | 静态织入 |
@within | 匹配包含某个注解的类中所有的连接点 | 静态织入 |
this | spring aop生成的代理类匹配this表达式中描述的类型 | 动态织入 |
target | 业务对象匹配target表达式中的描述类型 | 动态织入 |
@target | 匹配的目标对象的类有一个指定的注解 | 动态织入 |
args | 匹配连接点中的参数类型 | 动态织入 |
@args | 匹配连接点中的参数类型有指定注解的连接点 | 动态织入 |
@annotation | 匹配有指定注解的连接点 | 动态织入 |
execution
/***
* execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
*修饰符匹配(modifier-pattern?)
* 返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等
* 类路径匹配(declaring-type-pattern?)
* 连接点名匹配(name-pattern)可以指定连接点名 或者 *代表所有, set* 代表以set开头的所有连接点
* 参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“*”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的连接点;(*,String) 表示匹配有两个参数的连接点,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数
* 异常类型匹配(throws-pattern?)
* 其中后面跟着“?”的是可选项
*
* 第1个*代表任意返回类型
* ..代表service包及其子包
*第2个*代表任意类
*第3个*代表任意连接点
* (..)表示任意数量的参数
*/
@Pointcut("execution(* com.example.demo..*.*(..))")
public void log() {
}
within
/**
* 表达式格式:包名.* 或者 包名…*
* com.example.demo..* 切入包或子包中连接点
* com.example.demo.* 匹配包中的连接点,不包含子包
*/
@Pointcut("within(com.example.demo..*)")
public void log() {
}
@within
/***
*指定匹配必须包含某个注解的类里的所有连接点
* 可以理解为,这是对类的操作,当某个类申明了这个注解,则这个类中的所有连接点都会被切入
*/
@Pointcut("@within(com.example.demo.support.aop.LogFunc)")
public void log() {
}
this
/***
* 匹配指定的类型的对象
* 注意点:目标对象使用aop之后生成的代理对象必须是指定的类型才会被拦截,是目标对象被代理之后生成的代理对象和指定类型匹配才会被拦截
* @EnableAspectJAutoProxy:表示若spring创建的对象如果实现了接口,默认使用jdk动态代理,如果没有实现接口,使用cglib创建代理对象
* 可以使用@EnableAspectJAutoProxy(proxyTargetClass = true)处理这个问题
*/
@Pointcut("this(com.example.demo.service.generic.list.ListLearn)")
public void log() {
}
target
/***
* this 和 target 的不同点
* this作用于代理对象,target作用于目标对象
* this表示目标对象被代理之后生成的代理对象和指定的类型匹配会被拦截,匹配的是代理对象
* target表示目标对象和指定的类型匹配会被拦截,匹配的是目标对象
*/
@Pointcut("target(com.example.demo.service.generic.list.ListLearn)")
public void log() {
}
@target
/***
* 匹配的目标对象的类有一个指定的注解
* ,不知道原因
*/
@Pointcut("@target(com.example.demo.support.aop.LogFunc)")
public void log() {
}
args
/***
*匹配连接点中的参数类型
*/
@Pointcut("args(com.example.demo.bo.Banner)")
public void log() {
}
@args
/***
* 匹配连接点的参数类型上有注定注解的连接点
*/
@Pointcut("@args(com.example.demo.support.aop.LogFunc)")
public void log() {
}
@annotation
/***
* 匹配有指定注解的连接点
*/
@Pointcut("@annotation(com.example.demo.support.aop.LogFunc)")
public void log() {
}
- 组合切入点表达式
切入点表达式可以使用'&', '||' 和 '!'来组合。还可以通过名字来指向切入点表达式。以下的例子展示了三种切入点表达式: anyPublicOperation(在一个方法执行连接点代表了任意public方法的执行时匹配);inTrading(在一个代表了在交易模块中的任意的方法执行时匹配)和 tradingOperation(在一个代表了在交易模块中的任意的公共方法执行时匹配)。
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
@Pointcut("within(com.xyz.someapp.trading..*")
private void inTrading() {}
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}
spring官方文档:http://shouce.jb51.net/spring/aop.html
老螺丝:https://blog.youkuaiyun.com/swordcenter/article/details/76792497