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

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

问题描述

我已经调试了一段时间了,我希望有人能在这里有所启发.

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

我有一个 Maven 项目,它使用 JDK 1.6 添加到 Jenkins 中.我在这个项目中使用 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.

有什么想法吗?谢谢.

推荐答案

摘自上述问题评论:

您是否在 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天全站免登陆