spring 在Jenkins中出现Spring错误“名为'x'的Bean必须是[y]类型,但实际上是[$Proxy]类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8391944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins
提问by limc
I have been debugging this for awhile now, and I'm hoping someone could shed some light here.
我已经调试了一段时间了,我希望有人能在这里有所启发。
I have a Maven project that is added into Jenkins, using JDK 1.6. I'm using AOP in this project to handle the database transaction.
我有一个使用 JDK 1.6 添加到 Jenkins 中的 Maven 项目。我在这个项目中使用 AOP 来处理数据库事务。
When I run the build in Jenkins, my testcase fails with the following exceptions:-
当我在 Jenkins 中运行构建时,我的测试用例失败,但有以下异常:-
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataHandlerClassificationImpl':
Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
The DataHandlerClassificationImplclass looks something like this:-
这个DataHandlerClassificationImpl类看起来像这样:-
@Service
public class DataHandlerClassificationImpl extends DataHandler {
@Resource(name="writerDataLocationImpl")
private WriterData writerData;
...
}
WriterDatais an interface with multiple implementations.
WriterData是一个具有多种实现的接口。
I am able to execute the code without problem from the IDE. To determine whether it is a Maven problem or Jenkins problem, I navigated to the Jenkins' project job folder using command line and I'm able to run mvn testwithout any errors.
我能够从 IDE 毫无问题地执行代码。为了确定它是 Maven 问题还是 Jenkins 问题,我使用命令行导航到 Jenkins 的项目作业文件夹,并且我能够运行mvn test而没有任何错误。
I know the proxy error has something to do with AOP, and that I can only autowire to an interface instead of a concrete class... but that's not the case here since I'm able to run my code fine outside Jenkins.
我知道代理错误与 AOP 有关,而且我只能自动连接到接口而不是具体的类……但这里的情况并非如此,因为我能够在 Jenkins 之外很好地运行我的代码。
Any ideas? Thanks.
有任何想法吗?谢谢。
回答by Tomasz Nurkiewicz
Excerpt from question comments above:
摘自上述问题评论:
Are you running Cobertura, Sonar or other code-instrumenting tool on Jenkins? Note that mvn sitemight also be configured to include Cobertura report in generated site.
您是否在 Jenkins 上运行 Cobertura、Sonar 或其他代码检测工具?请注意,mvn site也可能配置为在生成的site.
The problem with Cobertura is that it performs pretty heavy byte-code instrumentation including the addition of some custom interfaces. When Spring starts up it generates proxies for beans. If bean has at least one interface, it uses standard Java proxy. Otherwise it tries to create class-based proxy.
Cobertura 的问题在于它执行非常繁重的字节码检测,包括添加一些自定义接口。当 Spring 启动时,它会为 bean 生成代理。如果 bean 至少有一个接口,它使用标准的 Java 代理。否则它会尝试创建基于类的代理。
I guess in your case the CGLIB class proxy was used but after Cobertura instrumentation Spring fall back to java proxies. This caused startup error because dependency injection expected class (or CGLIB subclass).
我猜在你的情况下使用了 CGLIB 类代理,但在 Cobertura 检测之后 Spring 回退到 Java 代理。这导致启动错误,因为依赖注入预期类(或 CGLIB 子类)。
To cut long story short, force CGLIB class proxies and you'll be fine:
长话短说,强制 CGLIB 类代理,你会没事的:
<aop:config proxy-target-class="true"/>

