如何在Spring语境中纠正bean的注入映射 [英] How to correct inject map of bean in spring context

查看:108
本文介绍了如何在Spring语境中纠正bean的注入映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring应用程序中使用组件扫描。所以在spring上下文中我创建了map:

I am using component-scan in my spring application. So in spring context I created map:

<util:map id="mapByName" map-class="java.util.concurrent.ConcurrentHashMap">
    <entry key="Name1" value-ref="MyCustomClassName1" />
</util:map>

在我的班级注释@Service我想注入这个属性:

and in my class annotated by @Service I want to inject this property:

@Inject
private Map<String, MyCustomClassName1> mapByName;

这仍然有效。只是以钥匙为名的问题。当我打印这个属性时,我得到了 [MyCustomClassName1=org.my.package.service.MyCustomClassName1@cb52f2]

this is still working. Problem just in name of key. When I print this property I got [MyCustomClassName1=org.my.package.service.MyCustomClassName1@cb52f2]

因此,您可以看到键的名称已从Name1-> MyCustomClassName1(此类的名称)更改。所以我的问题是如何在map属性中定义自定义键名?

so as you can see name of key is changed from Name1->MyCustomClassName1 (Name of this class). So my question is how to define custom key name in map property ?

推荐答案

引用来自文档


自动装配的Maps值将包含与预期类型匹配的所有bean实例,而Maps键将包含相应的bean名称。

An autowired Maps values will consist of all bean instances that match the expected type, and the Maps keys will contain the corresponding bean names.

我认为只需用 @Resource 更改 @Inject 即可。

I think that just changing @Inject with @Resource will do it.

这篇关于如何在Spring语境中纠正bean的注入映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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