问题描述
我有一个 bean,我想使用 Spring util namespace <util:list id="myList">
注入一个命名列表,但 Spring 正在寻找类型为 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 行为的一个相当模糊的部分,在 3.11.2.@自动连线:
This is due to a rather obscure part of @Autowired's behaviour, specified in 3.11.2. @Autowired:
也可以提供所有来自特定类型的豆类ApplicationContext
通过添加对字段或方法的注释期望该类型的数组...
这同样适用于类型化集合...
The same applies for typed collections...
换句话说,通过说 @Autowired @Qualifier("myList") List
,你实际上是在要求给我所有 java 类型的 bean 的列表".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".
如果您打算表达按名称注释驱动的注入,不要主要使用 @Autowired
- 即使如果在技术上能够参考通过 @Qualifier
到一个 bean 名称值.相反,更喜欢 JSR-250@Resource
注释是在语义上定义以识别一个特定目标组件由其唯一名称,具有声明的类型与匹配无关过程.
作为一个特定的后果语义差异,beans自己定义为集合或地图类型不能通过注入@Autowired
因为类型匹配不是适当地适用于他们. 使用@Resource
对于此类bean,参考特定的集合/映射 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 模式自动连接列表会产生 NoSuchBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!