Spring 不能自动装配 Map bean [英] Spring can't autowire Map bean
问题描述
我在 spring 中定义了一个地图:
I've defined a map in spring as such:
<util:map id="AdditionalParams" scope="prototype" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">
<entry key="Start" value="12345" />
<entry key="Finish" value="12365" />
</util:map>
然后我将这个 bean 自动装配到一个定义为的属性:
And then I'm autowiring this bean to a property defined as:
private @Autowired @Qualifier(value = "AdditionalParams") Map<String, String> additionalParams;
执行此操作时,会抛出异常:
When doing this, the an exception get's thrown saying that:
Caused by: org.springframework.beans.factory.BeanCreationException: Error create bean with name 'DutyCreator': Injection of autowired dependencies failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 java.util.Map DutyCreator.additionalParams;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有找到类型 [java.lang.String] 的匹配 bean 依赖 [map with value type java.lang.String]: 预计至少有 1 个 bean 有资格作为自动装配候选对于这种依赖.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=AdditionalParams)}
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DutyCreator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map DutyCreator.additionalParams; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type 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=AdditionalParams)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到类型为 [java.lang.String] 的匹配 bean 依赖 [map with value type java.lang.String]: 预计至少有 1 个符合条件的 bean作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=AdditionalParams)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency [map with value type 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=AdditionalParams)}
有什么想法吗?
干杯.
推荐答案
从 Spring 4.3 开始,@Autowired
可以注入列表和映射,问题中的给定代码将起作用:
Starting with Spring 4.3, @Autowired
can inject lists and maps and the given code in the question would work:
也就是说,从 4.3 开始,集合/映射和数组类型也可以通过 Spring 的 @Autowired
类型匹配算法进行匹配,只要 @ 中保留元素类型信息Bean
返回类型签名或集合继承层次结构.
That said, as of 4.3, collection/map and array types can be matched through Spring’s
@Autowired
type matching algorithm as well, as long as the element type information is preserved in@Bean
return type signatures or collection inheritance hierarchies.
但是对于较低的 Spring 版本,您无法自动装配这样的集合.但是,您可以执行以下操作:
But with a lower Spring version, you can't autowire a collection like that. However, you can do the following:
@Resource(name="AdditionalParams")
private Map<String, String> additionalParams;
甚至:
@Value("#{AdditionalParams}")
private Map<String, String> additionalParams;
检查 spring文档,提示部分:
本身定义为集合或映射类型的 bean 不能通过@Autowired 注入,因为类型匹配不正确适用于他们.对此类 bean 使用 @Resource
beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans
这篇关于Spring 不能自动装配 Map bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!