AspectJ加载时间编织不工作在Spring bean [英] AspectJ load time weaving not working on Spring beans

查看:124
本文介绍了AspectJ加载时间编织不工作在Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个项目,使用Java(不是xml)风味的Spring配置连接依赖项。它还有分析逻辑,应通过AspectJ编织到所需的方法(通过注释)。设置正在工作,我可以看到我的所需包裹被编织的类和分析信息被注销他们。



问题是,编织不适用于 @Bean 类。我已在 aop.xml 中启用调试:

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

我可以看到我想要的包中的类被编织,但不是配置中的bean。不幸的是,我不能在这里发布真正的代码,但是这里有一个简单的例子:

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

AnnotatedClass1 AnnotatedClass2 生活在同一个包中,编织工作直接实例化,而不是bean返回的。



我搜寻过 Spring AOP文档,但我似乎找不到与此相关的任何内容。有一些魔法你需要做的自动代理和一些限制SpringAOP,但加载时间编织应该只是工作,只要我能告诉 - 我试过私人方法,例如,它工作。

解决方案

问题是返回类型 - 如果我这样做:

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

编织也开始为bean工作。我初步的假设是,它与Spring缓存bean有关,而不是使用编织的版本,但这没有意义,因为:




  • 加载时间编织应该在行为,好...类加载时间:)。

  • 我实际上检查了Spring和AspectJ的调试输出,没有提到我的类,因此,



这是我第一次使用这个东西,所以我可能会误解的东西。如果任何人可以解释为什么 @Bean 方法的返回类型与编织有任何关系,我很乐意接受您的答案而不是这个答案。


I'm working on a project that uses the Java (not xml) flavour of Spring configuration for wiring up dependencies. It also has profiling logic that should be weaved via AspectJ onto the desired methods (via annotations). The setup is working and I can see classes from my desired package being weaved and profiling information being logged out of them.

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

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

And I can see classes in my desired package being weaved, but not the beans in the configuration. If I instantiate the classes directly (not inject them) weaving works.

Unfortunately, I can't post real code here, but here's a dumbed down 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 weaving works on the one instantiated directly and not the one returned by the bean.

I've searched through the Spring AOP docs but I can't seem to find anything related to this. There is some magic you need to do for auto-proxying and some limitations for SpringAOP but load time weaving should just work as far as I can tell - I've tried on private methods for example and it worked.

解决方案

The problem was the return type - if I do:

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

the weaving starts to work for the bean as well. My initial assumption was that it has something to do with Spring caching the bean and not using the weaved version, but this didn’t make sense because:

  • load time weaving is supposed to act at, well… class load time :). Then, it doesn’t matter what the method returns, the class should have the aspects.
  • I’ve actually checked the debug output for both Spring and AspectJ and no mention of my class so it must have been ignored somehow.

This is the first time I’ve used this stuff, so I might be misunderstanding things. If anyone can explain why the return type of the @Bean method has anything to do with weaving I’d be happy to accept your answer instead of this one.

这篇关于AspectJ加载时间编织不工作在Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆