The idea is to create annotations hierarchy (similar to @Service
, @Component
etc) using @AliasFor
annotation. This should give me the possibility to define aspect, that would execute on parent annotation, and every alias of it. But somehow it doesn't work for me.
@ComponentScan
is fine, @EnableAspectJAutoProxy
is set.
Example:
@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();
}
UPDATE: I've updated code, as @M.Deinum suggested in a comment below. But it still doesnt work.