Spring AOP: @annotation(annotation)
if you have the following Spring Bean:
@Component
public class foo {
@com.pkg.Bar
void fooMe() {
}
}
Then the following Advice:
@Around("@annotation(com.pkg.Bar)")
Will invoke the interceptor around fooMe
(or any other Spring bean method annotated with @Bar
)
The @Transactional
annotation is a good example
You would have a parameter named annotation
, of the appropriate type. It's called bound annotation, see this excerpt from the Spring AOP documentation:
The following example shows how you could match the execution of methods annotated with an @Auditable annotation, and extract the audit code.
First the definition of the
@Auditable
annotation:@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Auditable { AuditCode value(); }And then the advice that matches the execution of
@Auditable
methods:@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)") public void audit(Auditable auditable) { AuditCode code = auditable.value(); // ... }