我想对带有自定义注释的带注释类的所有方法执行简单的日志。我创建了下一个注释:
@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,结果相同。