Search code examples
springdependency-injectioncollectionsimplementation

spring collection implementation of injection


I am kinda new to Spring.

My Springframework version is 5.3.9.

What specific implementation does Spring choose to be injected?

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? Or Other implementation?
  • Does Spring allow choose of implementation class
  • Suppose the default implementation class will change with versions where could I find docs about this.

Many Thanks

My Efforts:

  • I read the spring-framewwork5.x doc and searched for collection implementation class but failed to find it.

  • I also try to read the spring-framework5.x sourcecode org.springframework.beans.factory.supportDefaultListableBeanFactory#resolveMultipleBeans But still fail to find the implementation class.

  • My Java is not excellent

  • Googled about it but didn't find


Solution

  • In this Spring 5.3.9 docs it stated that

    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.

    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:

    If no 'map-class' attribute is supplied, the container chooses a Map implementation.

    I'd suggest printing your map class name to figure out the default type.