正在为使用 @AliasFor 别名的注释执行 Spring 个 AOP 方面

Executing Spring AOP aspects for annotations aliased with @AliasFor

想法是使用 @AliasFor 注释创建注释层次结构(类似于 @Service@Component 等)。这应该让我有可能定义方面,这将在父注释上执行,以及它的每个别名。但不知何故,它对我不起作用。

@ComponentScan可以,@EnableAspectJAutoProxy已定。

示例:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface ParentAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParentAnnotation
public @interface ChildAnnotation {

    @AliasFor(annotation = ParentAnnotation.class)
    String value() default "";
}
@Aspect
@Component
public class EventRecorderAspect {

    @Around("@annotation(com.example.ParentAnnotation)")
    public void exampleMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        // This should be executed for both @ParentAnnotation and @ChildAnnotation
    }
}
@RestController
public class ExampleController {
    @ChildAnnotation // This should result in executing aspect for every implementation
    String controllerMethod(); 
}

更新: 正如@M.Deinum 在下面的评论中所建议的那样,我已经更新了代码。但是还是不行。

AspectJ 切入点匹配语法,其中一个子集被 Spring AOP 使用,不了解元注释,即使使用 Spring 框架本身在其他地方也支持它.我认为最接近于在切入点中指定元注释的方法是明确地执行到您需要的嵌套级别。请参阅 以获取显示

语法变体的示例
  • class级,例如within(@(@com.example.ParentAnnotation *) *),
  • 方法级,例如execution(@(@com.example.ParentAnnotation *) * *(..))

注释。


更新: @Ariel Grabijas 在后续评论中提问:

So is there a way to somehow inherit annotations from interface method to class method?

不在 Java 中,也不通过 Spring AOP。但是有一个.