0

I have an Annotation like this (in Spring Boot 2):

package com.test;

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

And a meta annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MyAnnotation 
public @interface MyEndpoint{ }

How can I define an Aspect in order to be executed before each method that has @MyAnnotation?

If I define it has below,

@Before("execution(public * *(..)) && @annotation(myAnnotation)")
public void authorize(JoinPoint pjp, MyAnnotation myAnnotation) { }

then the method authorize is called only

@MyAnnotation()
public myMethod(){}

and NOT in this case:

@MyEndpoint()
public myMethod() {}

thank you

Fabry
  • 1,498
  • 4
  • 23
  • 47

1 Answers1

0

@Inherited annotations only works from annotated classes to subclasses and not in any other case. I explained this in detail here and also show what an AspectJ workaround can look like. You can adapt it to your case. But you really need to use AspectJ in order to achieve this, not just Spring AOP.

kriegaex
  • 63,017
  • 15
  • 111
  • 202