Last active
July 26, 2017 10:47
-
-
Save attacco/9af2433f536638cde96a7204d16c9ff9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first, create custom qualifier annotation | |
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Qualifier | |
public @interface CustomAnn { | |
Class<?> any(); | |
} | |
// now create an annotation, that "inherits" from CustomAnn, specifying it's "any" property | |
@Target({ElementType.METHOD}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@CustomAnn(any = String.class) | |
public @interface SpecificCustomAnn { | |
} | |
// here we have to create a bean with tho methods: first is annotated with our SpecificCustomAnn, and second annotated with "base" CustomAnn | |
@Bean | |
class MyBean { | |
@SpecificCustomAnn | |
public void m1() { | |
// ... | |
} | |
@CustomAnn(any = Integer.class) | |
public void m2() { | |
// ... | |
} | |
} | |
// and, finally, declare an "around"-advice, that wrap invocations of all methods, annotated with @CustomAnn | |
@Component | |
@Aspect | |
public class CustomAnnAdvice { | |
@Around("@annotation(CustomAnn)") | |
private Object anyAnnotatedMethod(ProceedingJoinPoint pjp) throws Throwable { | |
return pjp.proceed(pjp.getArgs()); // do nothing | |
} | |
} | |
// Question: whether or not the method "m1" is wrapped with our CustomAnnAdvice? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment