简体   繁体   English

spring 采集实现注入

[英]spring collection implementation of injection

I am kinda new to Spring.我对 Spring 有点陌生。

My Springframework version is 5.3.9.我的Springframework版本是5.3.9。

What specific implementation does Spring choose to be injected? Spring选择被注入的具体实现是什么?

for example I have例如我有

// bean class

private Map<String,String> myMap;

public void setMyMap(Map<String, String> myMap) {
    this.myMap = myMap;
}
<!-- bean def xml -->
<bean id="student" class="com.my.bean.Student">
    <property name="myMap">
        <map>
            <entry key="name" value="someName"/>
            <entry key="school" value="someSchool"/>
        </map>
    </property>
</bean>

Questions:问题:

  • Does Spring choose HashMap as implementation to be injected? Spring 是否选择 HashMap 作为要注入的实现? Or Other implementation?或其他实施?
  • Does Spring allow choose of implementation class Spring 是否允许选择实现 class
  • Suppose the default implementation class will change with versions where could I find docs about this.假设默认实现 class 会随着版本的变化而变化,我在哪里可以找到关于这个的文档。

Many Thanks非常感谢

My Efforts:我的努力:

  • I read the spring-framewwork5.x doc and searched for collection implementation class but failed to find it.我阅读了 spring-framewwork5.x 文档并搜索了集合实现 class 但没有找到。

  • I also try to read the spring-framework5.x sourcecode org.springframework.beans.factory.supportDefaultListableBeanFactory#resolveMultipleBeans But still fail to find the implementation class.我也尝试阅读 spring-framework5.x 源代码org.springframework.beans.factory.supportDefaultListableBeanFactory#resolveMultipleBeans但仍然找不到实现 class。

  • My Java is not excellent我的 Java 不是很好

  • Googled about it but didn't find用谷歌搜索但没有找到

In this Spring 5.3.9 docs it stated that在此Spring 5.3.9 文档中,它指出

You can also explicitly control the exact type of Map that is instantiated and populated by using the 'map-class' attribute on the <util:map/> element.您还可以使用 <util:map/> 元素上的“map-class”属性显式控制实例化和填充的 Map 的确切类型。

Example:例子:

<util:map id="emails" map-class="java.util.TreeMap">
    <entry key="pechorin" value="pechorin@hero.org"/>
    <entry key="raskolnikov" value="raskolnikov@slums.org"/>
    <entry key="stavrogin" value="stavrogin@gov.org"/>
    <entry key="porfiry" value="porfiry@gov.org"/>
</util:map>

They don't state the default implementation, it's only stated that:他们没有 state 默认实现,它只是声明:

If no 'map-class' attribute is supplied, the container chooses a Map implementation.如果未提供“map-class”属性,容器将选择 Map 实现。

I'd suggest printing your map class name to figure out the default type.我建议打印您的 map class 名称以确定默认类型。

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

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