Java – when AspectJ is loaded, weaving cannot work on spring beans

I'm developing a project that uses the Java (not XML) style of spring configuration to connect dependencies It also has analysis logic that should be woven into the required methods (via annotations) through AspectJ The settings work well, I can see that the classes in the package I need are woven, and I can record the analysis information from it

The problem is that weaving has no effect on the @ bean class I work in AOP Debugging is enabled in XML:

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

I can see that the classes in the package I need are woven, but not the beans in the configuration If I instantiate classes directly (without injecting them), weaving works

Unfortunately, I can't publish real code here, but this is a stupid example:

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

Annotatedclass1 and annotatedclass2 exist in the same package. Weaving works on the directly instantiated one, not the one returned by the bean

I have searched the spring AOP docs, but I can't seem to find anything related to it You need to do some magic for automatic proxy and some limitations of spring AOP, but load time weaving should work as much as possible - for example, I've tried private methods and it works

Solution

The problem is the return type – if I do this:

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

Weaving also began to work for beans My initial assumption was that it was related to spring cache beans instead of using the weaved version, but it didn't make sense because:

>Load time weaving should work, and... Class load time:) Then, it doesn't matter what the method returns. The class should have aspects. > I actually checked the debug output of spring and AspectJ without mentioning my class, so it must be ignored in some way

This is the first time I use these things, so I may misunderstand things If someone can explain why the return type of @ bean method is related to weaving, I'd be happy to accept your answer, not this

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>