得到Spring错误“名为"x"的Bean必须是类型[y],但实际上是类型[$ Proxy]".在詹金斯 [英] Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins

查看:101
本文介绍了得到Spring错误“名为"x"的Bean必须是类型[y],但实际上是类型[$ Proxy]".在詹金斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经调试了一段时间,我希望有人可以在这里阐明一些问题.

I have been debugging this for awhile now, and I'm hoping someone could shed some light here.

我有一个使用JDK 1.6添加到Jenkins中的Maven项目.我在这个项目中使用AOP来处理数据库事务.

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.

当我在Jenkins中运行构建时,我的测试用例失败,但有以下例外:-

When I run the build in Jenkins, my testcase fails with the following exceptions:-

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]
    ...
    ...

DataHandlerClassificationImpl类看起来像这样:-

@Service
public class DataHandlerClassificationImpl extends DataHandler {

    @Resource(name="writerDataLocationImpl")
    private WriterData writerData;

    ...
}       

WriterData是具有多种实现的接口.

WriterData is an interface with multiple implementations.

我能够在IDE中毫无问题地执行代码.为了确定这是Maven问题还是Jenkins问题,我使用命令行导航到Jenkins的项目作业文件夹,并且能够运行mvn test且没有任何错误.

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 test without any errors.

我知道代理错误与AOP有关,并且我只能自动连接到接口而不是具体的类……但是这里不是这种情况,因为我可以在Jenkins外部很好地运行我的代码

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.

有什么想法吗?谢谢.

Any ideas? Thanks.

推荐答案

以上问题注释的摘录:

您是否在Jenkins上运行Cobertura,Sonar或其他代码工具?请注意,mvn site也可能配置为在生成的site中包含Cobertura报告.

Are you running Cobertura, Sonar or other code-instrumenting tool on Jenkins? Note that mvn site might also be configured to include Cobertura report in generated site.

Cobertura的问题在于它执行相当繁重的字节码检测,包括添加一些自定义接口.当Spring启动时,它将为bean生成代理.如果bean具有至少一个接口,则它将使用标准的Java代理.否则,它将尝试创建基于类的代理.

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.

在您的情况下,我猜想使用了CGLIB类代理,但是在Cobertura规范化之后,Spring回归了Java代理.这导致启动错误,因为依赖项注入期望类(或CGLIB子类).

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类代理,您会没事的:

To cut long story short, force CGLIB class proxies and you'll be fine:

<aop:config proxy-target-class="true"/>

这篇关于得到Spring错误“名为"x"的Bean必须是类型[y],但实际上是类型[$ Proxy]".在詹金斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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