I'm writing a plugin for an existing application and I need to add an "After returning" advice to the certain method. aspectj-autoproxy is currently disabled.
To avoid possible side effects for existing classes that have AspectJ annotations that are currently ignored I'd like to enable these annotations handling only for my bean. However, @EnableAspectJAutoProxy
affects the entire context.
The only thing that comes to my mind is to manually construct the proxy using ProxyFactory
, but after this I'll have two beans and I won't be able to use AspectJ expressions.
@Configuration
public class MyConf {
@Primary
@Bean
public SomeBean getSomeBean(@Autowired @Qualifier("SomeBeanImpl") target) {
ProxyFactory factory = new ProxyFactory(target);
// setup the advice, the method filter etc.
return (SomeBean)factory.getProxy();
}
Is there some other set of annotations to create advices without globally enabling aspectj-autoproxy?
Upd
Looks like I need to dig towards manually registering another AnnotationAwareAspectJAutoProxyCreator
instance with includePatterns
property set.
EnableAspectJAutoproxy
is the instruction to enable aspect processing and handling and works per context that is simply not changeable. which beans an aspect applies to is what a pointcut is for which you should use. Unless you manually configure each and every aspect (and manually create the aspect creation logic) but then again what is the point of writing a pointcut/aspect if you do that? Basically beats the purpose of it. – M. Deinum