AspectJ time rewind time not working on spring beans

I am working on a project that uses the Spring (not xml) Spring configuration adapter to connect dependencies. It also has profiling logic that must be compressed through AspectJ to the required methods (via annotations). The setup works, and I see how the classes from my desired package are woven and the profiling data comes out of them.

The problem is that weaving does not work for @Bean classes. I have included debug in aop.xml via:

 <weaver options="-XnoInline -Xreweavable -verbose -debug -showWeaveInfo"> 

And I see that the classes in my desired package are woven, but not beans in the configuration. If I instantiate the classes directly (without pasting them), the weaving works.

Unfortunately, I cannot post the real code here, but here is an example:

 @Configuration @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED) public class MySpringConfig { @Bean AnnotatedClass1 annotatedClass1() { return new AnnotatedClass1(new AnnotatedClass2()); } } 

AnnotatedClass1 and AnnotatedClass2 live in the same package, and the weaves runs on an instance created directly, and not the one returned by the bean.

I looked through Spring AOP docs , but I can not find anything related to this. There is some magic for automatic proxying and some limitations for SpringAOP, but the time taken to load should work as far as I can tell - for example, I tried to use private methods, and it worked.

+7
java spring aop aspectj configuration
source share
1 answer

The problem was the inverse type - if so:

 @Bean Object annotatedClass1() { return new AnnotatedClass1(new AnnotatedClass2()); } 

weaving starts to work for bean too. My initial assumption was that it has something to do with Spring bean caching and doesn't use the woven version, but that didn't make sense, because:

  • the load time that is supposed to be affected should be ... the class load time :). Then, no matter what the method returns, the class should have aspects.
  • I really checked the debug output for both Spring and AspectJ, and didn't mention my class, so it was somehow ignored.

This is the first time I have used this material, so I might not understand things. If anyone can explain why the return type of the @Bean method has anything to do with weaving, Id would be happy to accept your answer instead.

+3
source share

All Articles