Skip to content

Instantly share code, notes, and snippets.

@madan712
Created January 3, 2013 15:20
Show Gist options
  • Save madan712/4444239 to your computer and use it in GitHub Desktop.
Save madan712/4444239 to your computer and use it in GitHub Desktop.
Spring - How to inject List/ Map using bean? We have a class Country.java with two instance variables city and president. We will set those values using applicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="country" class="com.javaxp.Country">
<property name="city" ref="CityMap"></property>
<property name="president" ref="PresidentList"></property>
</bean>
<bean id="CityMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="China" value="Beijing" />
<entry key="India" value="Delhi" />
<entry key="United States" value="Washington" />
<entry key="United Kingdom" value="London" />
</map>
</constructor-arg>
</bean>
<bean id="PresidentList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>Hu Jintao</value>
<value>Pranab Mukherjee</value>
<value>Barack Obama</value>
<value>David Cameron</value>
</list>
</constructor-arg>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment