开发者

How to inject a Map<String, List> in java springs?

开发者 https://www.devze.com 2023-02-18 09:33 出处:网络
How to inject a Map in java spring framework? If po开发者_StackOverflow中文版ssible please provide some sample code.

How to inject a Map in java spring framework? If po开发者_StackOverflow中文版ssible please provide some sample code.

Is the following legal?

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


Define a Map like this first inside your 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>

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

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


I think your syntax is not legal as spring throws org.xml.sax.SAXParseException when processing the bean configuration xml .

It should work after removing the <value> tag around the <list>.

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

             <list>
                    <value>String</value>
                    <value>String</value>
             </list>
        </entry>
    </map> 
</property>


This is my example:

<bean class="com.common.handlermgmnt.HandlerMapAdder">
    <constructor-arg index="0" type="java.util.Map">
        <map key-type="java.lang.String" value-type="com.common.ViewWidget">
            <entry key="DefaultView">
                <bean class="com.common.DefaultViewWidget"/>
            </entry>
            <entry key="AnotherView">
                <bean class="com.common.AnotherViewWidget"/>
            </entry>
        </map>
    </constructor-arg>
    <constructor-arg index="1" type="com.common.handlermgmnt.HandlerManager" ref="widget_handlerManager"/>
</bean>


Inject it using SpEL. #{id}. this works for me.

in the .xml:

<util:map id="roleLocationMap">
    <entry key="ROLE_ADMIN" value-ref="listA" />
    <entry key="ROLE_USER" value-ref="listB" />
</util:map>

in the .java

@Autowired
public MainController(
        @Value("#{roleLocationMap}") final Map<String, List<String>> roleLocationMap) {
    this.roleLocationMap = roleLocationMap;
}


Just ran into this case myself. If no need to reuse the list values as independent beans elsewhere, you could use this shorter version, without using 'value-ref':

<util:map id="mymap">
    <entry key="key1">
        <util:list>
            <value>val1</value>
            <value>val2</value>
        </util:list>
    </entry>
    <entry key="key2">
        <util:list>
            <value>val2</value>
            <value>val3</value>
            <value>val4</value>
        </util:list>
    </entry>
</util:map> 

And, wire it up in your Java code like this:

@Resource(name="mymap")
Map<String, List<String>> mapKey_List;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号