简体   繁体   English

Spring无法自动连线Map Bean

[英]Spring can't autowire Map bean

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: 然后,我将该bean自动装配为定义为的属性:

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; 由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为'DutyCreator'的bean时出错:自动连接依赖项的注入失败; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map DutyCreator.additionalParams; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有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. 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项[值类型为java.lang.String的映射]的匹配类型为[java.lang.String]的匹配的bean:期望至少有1个有资格作为自动装配候选的bean对于这种依赖性。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=AdditionalParams)} 依赖注释:{@ 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. 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项[值类型为java.lang.String的映射]的匹配类型为[java.lang.String]的bean:预期至少有1个有资格作为自动装配候选的bean对于这种依赖性。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=AdditionalParams)} 依赖注释:{@ 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: 从Spring 4.3开始,@ @Autowired 可以注入列表和地图 ,问题中的给定代码将起作用:

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. 也就是说,从4.3版本开始,只要元素类型信息保留在@Bean返回类型签名或集合继承层次结构中,就可以通过Spring的@Autowired类型匹配算法来匹配集合/映射和数组类型。

But with a lower Spring version, you can't autowire a collection like that. 但是在较低的Spring版本中,您无法自动连接这样的集合。 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: 检查spring文档 ,提示部分:

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. 本身定义为集合或映射类型的bean不能通过@Autowired注入,因为类型匹配不适用于它们。 Use @Resource for such beans 将@Resource用于此类bean

Seems like your @Qualifier(value = "AdditionalParams") is not working. 好像您的@Qualifier(value =“ AdditionalParams”)无法正常工作。

Try using the map by following annotation : 通过以下注释尝试使用地图:

@Resource
private Properties AdditionalParams;

and keeping your applicationContext.xml file intact. 并保持您的applicationContext.xml文件完整。

@Autowired ApplicationContext ctx;
private  <T> T getBean(String qualifier, Class<T> returnType){
    //use this for loop to print all bean from ctx. so you wont miss the typo.
    /*for(String s:ctx.getBeanDefinitionNames())
        log.info(s);*/
    return ctx.getBean(qualifier, returnType);
}

// inside your call //在通话中

 if(providerList == null){
       providerList = ctx.getBean("providerList", Map.class);
 }

This Solution works good to me 该解决方案对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM