I need to intercept annotated methods using spring-aop. I already have the interceptor, it implements MethodInterceptor from AOP Alliance.
Here is the code:
@Configuration
public class MyConfiguration {
// ...
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
// ...
}
public class MyInterceptor implements MethodInterceptor {
// ...
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
//does some stuff
}
}
From what I've been reading it used to be that I could use a @SpringAdvice annotation to specify when the interceptor should intercept something, but that no longer exists.
Can anyone help me?
Thanks a lot!
Lucas
MethodInterceptor
can be invoked by registering a Advisor
bean as shown below.
@Configurable
@ComponentScan("com.package.to.scan")
public class AopAllianceApplicationContext {
@Bean
public Advisor advisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("@annotation(com.package.annotation.MyAnnotation)");
return new DefaultPointcutAdvisor(pointcut, new MyInterceptor());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With