Java 使用 util 模式自动连接列表会产生 NoSuchBeanDefinitionException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1363310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-wiring a List using util schema gives NoSuchBeanDefinitionException
提问by Paul McKenzie
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:
我有一个 bean,我想使用 Spring util 命名空间注入命名列表, <util:list id="myList">
但 Spring 正在寻找 String 类型的 bean 集合。我的坏测试是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ListInjectionTest {
@Autowired @Qualifier("myList") private List<String> stringList;
@Test public void testNotNull() {
TestCase.assertNotNull("stringList not null", stringList);
}
}
My context is:
我的上下文是:
<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>
But I get
但我得到
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.
这让我感到困惑,因为我认为这将是预期的工作方式。
采纳答案by skaffman
This is due to a rather obscure part of @Autowired's behaviour, specified in 3.11.2. @Autowired:
这是由于@Autowired 行为的一个相当模糊的部分,在3.11.2 中指定。@自动连线:
It is also possible to provide all beans of a particular type from the
ApplicationContext
by adding the annotation to a field or method that expects an array of that type...The same applies for typed collections...
还可以
ApplicationContext
通过将注释添加到需要该类型数组的字段或方法来提供特定类型的所有 bean ...这同样适用于类型化集合......
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("myList") List<String>
,您实际上是在要求“给我java.lang.String
具有限定符“myList”的所有类型 bean 的列表。
The solution is mentioned in 3.11.3. Fine-tuning annotation-based autowiring with qualifiers:
解决方法在3.11.3中提到。使用限定符微调基于注释的自动装配:
If you intend to express annotation-driven injection by name, do not primarily use
@Autowired
- even if is technically capable of referring to a bean name through@Qualifier
values. Instead, prefer the JSR-250@Resource
annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.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.
如果您打算按名称表示注解驱动的注入,请不要主要使用
@Autowired
- 即使技术上能够通过@Qualifier
值引用 bean 名称。相反,更喜欢 JSR-250@Resource
注释,该注释在语义上定义为通过其唯一名称标识特定目标组件,声明的类型与匹配过程无关。作为这种语义差异的一个特定结果,本身被定义为集合或映射类型的 bean 不能通过注入,
@Autowired
因为类型匹配不适用于它们。使用@Resource
的唯一名称等豆类,指的是特定集合/图豆。
So use this in your test, and it works fine:
所以在你的测试中使用它,它工作正常:
@Resource(name="myList") private List<String> stringList;
回答by juliangonzalez
Another thing that could be happening is that you are autowiring a property of a bean. In such case you dont need to autowire it, but just create the setter method and use the property tag in the bean definition (when using xml) example:
可能发生的另一件事是您正在自动装配 bean 的属性。在这种情况下,您不需要自动装配它,而只需创建 setter 方法并在 bean 定义中使用属性标记(使用 xml 时)示例:
<bean id="cleaningUpOldFilesTasklet" class="com.example.mypackage.batch.tasklets.CleanUpOldFilesTasklet">
<property name="directoriesToClean">
<list>
<value>asfs</value>
<value>fvdvd</value>
<value>sdfsfcc</value>
<value>eeerer</value>
<value>rerrer</value>
</list>
</property>
</bean>
And the class:
和班级:
public class CleanUpOldFilesTasklet extends TransferingFilesTasklet implements Tasklet{
private long pastMillisForExpiration;
private final String dateFormat = "MM.dd";
Date currentDate = null;
List<String> directoriesToClean;
public void setDirectoriesToClean(List<String> directories){
List<String> dirs = new ArrayList<>();
for(String directory : directories){
dirs.add(getSanitizedDir(directory));
}
this.directoriesToClean = dirs;
}
See, no @Autowired
annotation in class.
看,@Autowired
课堂上没有注释。