Home > database >  spring collection implementation of injection
spring collection implementation of injection

Time:12-08

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" >
    <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

CodePudding user response:

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->
    <entry key="pechorin" value="[email protected]"/>
    <entry key="raskolnikov" value="[email protected]"/>
    <entry key="stavrogin" value="[email protected]"/>
    <entry key="porfiry" value="[email protected]"/>
</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.

  • Related