问题描述
先上代码
aop自定义类
@Aspect
@Component
@Slf4j
public class LogAop {
@Pointcut("@annotation(com.demo.aop.MyLog")
public void MyLog() {
}
....
}
aop注解,作用在类、接口或枚举类上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface MyLog{
String value() default "";
}
这样设置,是不起作用的。因为@Pointcut里的参数值有问题。看下PointCut里的参数值各代表啥意思
execution:用于匹配方法执行的连接点
within:用于匹配指定类型内的方法执行
this:用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也* 类型匹配
target:用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配
args:用于匹配当前执行的方法传入的参数为指定类型的执行方法
@within:用于匹配所以持有指定注解类型内的类和方法
@target:用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解
@args:用于匹配当前执行的方法传入的参数持有指定注解的执行
@annotation:用于匹配当前执行方法持有指定注解的方法
bean:Spring AOP扩展的,AspectJ没有对于指示符,用于匹配特定名称的Bean对象的执行方法
问题原因
因为@annotation是匹配方法的,无法匹配类,所以应使用within
解决办法
@Aspect
@Component
@Slf4j
public class LogAop {
@Pointcut("@within (com.demo.aop.MyLog")
public void MyLog() {
}
....
}