当bean在另一个jar文件中时,Spring @Qualifier无法正常工作 [英] Spring @Qualifier not working when bean is in another jar file

查看:273
本文介绍了当bean在另一个jar文件中时,Spring @Qualifier无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多Spring Bean,其中一些在共享库jar中。我似乎无法使用 @Qualifier

I have a number of Spring beans, some of which are in a shared library jar. I can't seem to get @Qualifier to work.

我将default-autowire设置为 byType ,它使用的是Spring 3.1.0.M2,并作为独立的可执行文件运行。如果我从共享库中删除 TestTwoBean,则项目将按预期执行。

I have default-autowire set to "byType", this is using Spring 3.1.0.M2 and running as a standalone executable. If I remove "TestTwoBean" from the shared library the project executes as expected.

myproj-shared-lib.jar:

@Service
public class TestOneBean implements ITestBean {
}

@Service
public class TestTwoBean implements ITestBean {
}

myproj.jar:

@Service
public class TestConsumerBean {

    @Autowired @Qualifier("testOneBean")
    private ITestBean bean;

}

我得到没有唯一名称的bean 运行时异常:


org.springframework.beans.factory.UnsatisfiedDependencyException:
创建名称为bean的错误在文件[-]中定义的'testConsumerBean':
通过bean属性'bean'表达的不满意依赖性::没有定义[com.myco.ITestBean]类型的
唯一bean:预期的单个
匹配的Bean,但发现2:[testOneBean,testTwoBean];嵌套的
异常是
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义类型[com.myco.TestBean]的
个唯一的Bean:期望的单个
匹配的Bean,但是找到2:[testOneBean,testTwoBean]位于
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1167)
...

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testConsumerBean' defined in file [-]: Unsatisfied dependency expressed through bean property 'bean': : No unique bean of type [com.myco.ITestBean] is defined: expected single matching bean but found 2: [testOneBean, testTwoBean]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.myco.TestBean] is defined: expected single matching bean but found 2: [testOneBean, testTwoBean] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1167) ...

在这种情况下 @Qualifier 不起作用吗?有已知的解决方法吗?

Does @Qualifier not work in this situation? Is there a known workaround?

推荐答案

您确定要通过类型AND注释注入使用自动装配吗?按类型自动装配意味着即使未标注注入,spring也会尝试使用按类型查找来注入检测到的setter和构造函数参数。

Are you sure you want to use autowire by type AND annotation injection? Autowire by type means spring will attempt to inject detected setters and constructor parameters using by type lookup even if they aren't annotated for injection.

按名称注入字段。带有 @Service 的带注释的类将生成名称分别默认为类名 testOneBean和 testTwoBean的bean。 @Qualifier 使用Bean名称作为正确的匹配项。推荐的按名称注入方法是使用 @Resource(name = testOneBean)。我只能猜测由于将自动装配模式设置为按类型(我怀疑您确实需要这样做),spring会尝试按类型进行注入。

At the same time you are trying to inject fields by name. Your @Service annotated classes produce beans with names defaulting to the class name, "testOneBean" and "testTwoBean" respectively. @Qualifier uses bean names as correct matches. The recommended way of doing "by name" injection though is by using @Resource(name="testOneBean"). I can only guess spring tries injection by type due to autowire mode set to by type (which I doubt you really need).

我建议恢复为默认自动接线模式,并使用 @Resource 进行名称接线。

I would recommend reverting to default autowire mode and using @Resource for wiring by name.

这篇关于当bean在另一个jar文件中时,Spring @Qualifier无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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