问题描述
我有一个bean,我想使用Spring util命名空间注入一个命名列表< util:list id =myList>
但Spring正在寻找而是String类型的bean的集合。我的破解测试是:
I have a bean that i want to inject with a named list using Spring util namespace <util:list id="myList">
but Spring is looking for a collection of beans of type String instead. My broken test is:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ListInjectionTest {
@Autowired @Qualifier("myList") private List<String> stringList;
@Test public void testNotNull() {
TestCase.assertNotNull("stringList not null", stringList);
}
}
我的上下文是:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<util:list id="myList">
<value>foo</value>
<value>bar</value>
</util:list>
</beans>
但我得到了
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [collection of java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myList)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:726)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:571)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
令我困惑的是因为我认为这将是预期的方式。
Which puzzles me rather as I figured this would be the way it was expected to work.
推荐答案
这是由于@Autowired的一个相当模糊的部分行为,在:
This is due to a rather obscure part of @Autowired's behaviour, specified in 3.11.2. @Autowired:
同样适用于打印的集合...
The same applies for typed collections...
换句话说,通过说 @Autowired @Qualifier(myList)List< String>
,你实际上要求给我所有类型 bean的列表java.lang.String
具有限定符myList。
In other words, by saying @Autowired @Qualifier("myList") List<String>
, you're actually asking for "give me the list of all beans of type java.lang.String
that have the qualifier "myList".
解决方案在:
作为此$ b $的特定结果b语义差异,
自己定义为集合或
地图类型的bean不能通过
@Autowired
注入,因为类型匹配是不是
适用于它们。对于这样的bean使用
@Resource
,引用
特定的集合/映射bean
唯一名称。
As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via @Autowired
since type matching is not properly applicable to them. Use @Resource
for such beans, referring to the specific collection/map bean by unique name.
所以在你的测试中使用它,它运行正常:
So use this in your test, and it works fine:
@Resource(name="myList") private List<String> stringList;
这篇关于使用util模式自动连接List会产生NoSuchBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!