【发布时间】:2019-11-19 12:06:29
【问题描述】:
我想对带有自定义注释的注释类的所有方法执行简单的日志记录。我创建了下一个注释:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface FooAnnotation {}
它用于随机类:
@FooAnnotation
public class Bar{
...
}
当然我的开始类有@EnableAspectJAutoProxy 注解
@EnableAspectJAutoProxy
@SpringBootApplication
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication.class, args);
}
}
在相关的 AspectJ 中,我需要定义一个 @Pointcut 来对所有使用 @FooAnnotation注释的类的所有方法执行 @After >
我尝试了下一个 AspectJ,但它不起作用
@Slf4j
@Aspect
@Component
public class FooAspectJ{
@Pointcut("within(x.y.z.FooNotify)")
private void annotatedWithin() {}
@Pointcut("@within(x.y.z.FooNotify)")
private void annotatedAtWithin() {}
@Pointcut("@annotation(x.y.z.FooNotify)")
private void annotatedAtAnnotation() {}
@After("annotatedWithin() || annotatedAtWithin() || annotatedAtAnnotation()")
private void afterAnnotation(JoinPoint joinPoint){
log.info("Executed annotated {}", joinPoint.getSignature().getName());
}
}
我看到了类似的帖子,比如 @AspectJ pointcut for all methods of a class with specific annotation 和 aspectj pointcut with annotation parameters,结果相同。
【问题讨论】:
标签: java spring aop aspectj aspect