I'm wondering if it's okay to create my own Spring Advisor via Java configuration, similar to what's proposed here. I too wanted to have annotated methods intercepted.
I had such an Advisor defined in our application, but after removing a completely unrelated @Configuration class, the whole thing collapsed and the advice no longer worked. After a lot of debugging, I figured that this could work
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public Advisor sessionContextAdvisor(OpenSessionInterceptor openSessionInterceptor)
{
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("@annotation(my.app.SessionContext)");
return new DefaultPointcutAdvisor(pointcut, openSessionInterceptor);
}
and indeed, that worked. I needed to declare the bean as an infrastructure bean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) for Spring. I gotta say, this feels even more hackey now.
On the other hand, there is a way to add custom Advisors via XML, using <aop:advisor> ..., as documented in the Spring docs. But I couldn't find a Spring-documented way to register an Advisor via Java configuration.
Also, Spring JavaDoc has to say this about the Advisor interface:
This interface is not for use by Spring users, but to allow for commonality in support for different types of advice.
That doesn't sound like you're supposed to create instances of this interface. But if there's an XML-way, is there really no Java configuration way to achieve the same thing? Is my solution not so bad after all?