Spring无法自动连线Map bean [英] Spring can't autowire Map bean

查看:168
本文介绍了Spring无法自动连线Map bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在春季定义了一个地图:

 < util:map id =AdditionalParamsscope = prototypemap-class =java.util.HashMap
key-type =java.lang.Stringvalue-type =java.lang.String>

< entry key =Startvalue =12345/>
< entry key =Finishvalue =12365/>
< / util:map>

然后我将这个bean自动连接到一个定义为:

  private @Autowired @Qualifier(value =AdditionalParams)Map< String,String> additionalParams; 

当这样做时,抛出异常,说:


导致:org.springframework.beans.factory.BeanCreationException:创建名称为DutyCreator的bean时出错:自动连线依赖关系的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private java.util.Map DutyCreator.additionalParams;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[java.lang.String]的匹配bean,用于依赖关系[值为java.lang.String类型的值]:至少有1个bean符合autowire候选为此依赖。依赖关系注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}



导致:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[java.lang.String]的匹配bean,用于依赖关系[值为java.lang.String]的值:至少有一个bean它符合这种依赖性的自动线路候选。依赖关系注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}


<

任何想法?



干杯。

解决方案

从Spring 4.3开始, @Autowired 可以注入列表和映射,该问题中的给定代码将起作用:


说,从4.3开始,可以通过Spring的 @Autowired 类型匹配来匹配收集/映射和数组类型算法,只要元素类型信息保存在 @Bean 中返回类型签名或集合继承层次结构。


但是使用较低的Spring版本,您无法自动连接一个集合在。但是,您可以执行以下操作:

  @Resource(name =AdditionalParams)
private Map< String,串GT; additionalParams;

甚至:

  @Value(#{AdditionalParams})
private Map< String,String> additionalParams;

检查 spring docs ,提示部分:


自己定义为集合或地图类型的bean不能通过@Autowired注入
,因为类型匹配不适用于
。对此类bean使用@Resource



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>

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 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: 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)}

Any ideas?

Cheers.

解决方案

Starting with Spring 4.3, @Autowired can inject lists and maps and the given code in the question would work:

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.

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;

or even:

@Value("#{AdditionalParams}")
private Map<String, String> additionalParams;

Check the spring docs, the tips section:

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屋!

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