如何注入Map< String,List>在java spring?

问题描述:

如何在java spring框架中注入Map?
如果可能,请提供一些示例代码。

How to inject a Map in java spring framework? If possible please provide some sample code.

以下是否合法?

<property name="testMap">
    <map>
        <entry>
            <key>
                <value>test</value>
            </key>
            <value>
                <list>
                    <value>String</value>
                    <value>String</value>
                </list>
            </value>
        </entry>
    </map> 
</property>


首先在 applicationContext.xml :

<util:list id="list1">
    <value>foo@bar.com</value>
    <value>foo1@bar.com</value>
</util:list>

<util:list id="list2">
    <value>foo2@bar.com</value>
    <value>foo3@bar.com</value>
</util:list>

<util:map id="emailMap" value-type="java.util.List">
    <!-- Map between String key and List -->
    <entry key="entry1" value-ref="list1" />
    <entry key="entry2" value-ref="list2" />
    ...
</util:map>

然后在你的任何bean中使用这个地图:

Then use this Map in any bean of yours like this:

<bean id="myBean" class="com.sample.beans">
    <property name="emailMap" ref="emailMap" />
</bean>