Tuesday 7 March 2017

Spring: Inject map of beans

In my previous post, I explained how to inject Map<String, String>. By using <map>, <entry> elements, value-ref attributes, you can inject map of beans.

Example
 <bean id="countryCodes" name="countryCodes" class="com.sample.pojo.Countries">
  <property name="countriesMap">
   <map>
    <entry key="91" value-ref="india" />
    <entry key="7" value-ref="russia" />
   </map>
  </property>
 </bean>

Following is the complete working application.

Country.java
package com.sample.pojo;

public class Country {
 private String name;
 private String capital;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getCapital() {
  return capital;
 }

 public void setCapital(String capital) {
  this.capital = capital;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Country [name=").append(name).append(", capital=").append(capital).append("]");
  return builder.toString();
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((capital == null) ? 0 : capital.hashCode());
  result = prime * result + ((name == null) ? 0 : name.hashCode());
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Country other = (Country) obj;
  if (capital == null) {
   if (other.capital != null)
    return false;
  } else if (!capital.equals(other.capital))
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  return true;
 }

}

Countries.java
package com.sample.pojo;

import java.util.Map;

public class Countries {
 private Map<Integer, Country> countriesMap;

 public Map<Integer, Country> getCountriesMap() {
  return countriesMap;
 }

 public void setCountriesMap(Map<Integer, Country> countriesMap) {
  this.countriesMap = countriesMap;
 }

}

myConfiguration.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="india" class="com.sample.pojo.Country">
  <property name="name" value="India" />
  <property name="capital" value="New Delhi" />
 </bean>

 <bean id="russia" class="com.sample.pojo.Country">
  <property name="name" value="Russia" />
  <property name="capital" value="Moscow" />
 </bean>

 <bean id="countryCodes" name="countryCodes" class="com.sample.pojo.Countries">
  <property name="countriesMap">
   <map>
    <entry key="91" value-ref="india" />
    <entry key="7" value-ref="russia" />
   </map>
  </property>
 </bean>

</beans>

HelloWorld.java

package com.sample.test;

import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Countries;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Countries countries = context.getBean("countryCodes", Countries.class);

  Set<Integer> countryCodes = countries.getCountriesMap().keySet();

  for (Integer countryCode : countryCodes) {
   System.out.println(countryCode + " : " + countries.getCountriesMap().get(countryCode));
  }

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java application, you can able to see following output.

91 : Country [name=India, capital=New Delhi]
7 : Country [name=Russia, capital=Moscow]





Previous                                                 Next                                                 Home

1 comment: