spring 可以用Spring创建HashMap但不能创建Map

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13948921/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:36:16  来源:igfitidea点击:

Can create HashMap with Spring but can't create Map

spring

提问by Kiva

I can do this in my applicationContext with Spring (3.0.5):

我可以在我的 applicationContext 中使用 Spring (3.0.5) 执行此操作:

<bean id="map" class="java.util.HashMap" scope="prototype" >
    <constructor-arg>
        <map key-type="java.lang.String" value-type="java.lang.String">
            <entry key="Key 1" value="1" />
            <entry key="Key 2" value="2" />
        </map>
    </constructor-arg>
</bean>

And in my controller, I can autowired my map like this:

在我的控制器中,我可以像这样自动连接我的地图:

@Autowired
@Qualifier("map")
private HashMap<String, String> map;

It works fine, but if I do this:

它工作正常,但如果我这样做:

@Autowired
@Qualifier("map")
private Map<String, String> map;

I get that:

我明白了:

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=map)}

引起: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=map)}

My question is: Why I can't autowired my map with the interface when I can with the implementation ?

我的问题是:当我可以使用实现时,为什么我不能使用接口自动装配我的地图?

Thanks.

谢谢。

回答by Viral Patel

While declaring a bean of type collection, one cannot inject it via @Autowired. See below documentation from Spring:

在声明类型为 collection 的 bean 时,不能通过 @Autowired 注入它。请参阅以下 Spring 文档:

4.11.3 Fine-tuning annotation-based autowiring with qualifiers

4.11.3 使用限定符微调基于注解的自动装配

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.

作为这种语义差异的特定结果,本身定义为集合或映射类型的 bean 不能通过 @Autowired 注入,因为类型匹配不适用于它们。对此类 bean 使用 @Resource,通过唯一名称引用特定的集合/映射 bean。

Thus instead of @Autowired, use @Resource:

因此,而不是@Autowired,使用@Resource

@Resource
@Qualifier("map")
private Map<String, String> map;

回答by Stanley

Try to use @Resourceinstead of @Autowired

尝试使用@Resource代替@Autowired

@Resource(name="map") 
private HashMap<String, String> map;

Check out the tip in 3.9.3 Fine-tuning annotation-based autowiring with qualifiersof Spring's documentation

查看3.9.3 Fine-tuning annotation-based autowiring with qualifiersof Spring 文档中的提示