A Developer's Diary

Dec 24, 2012

Spring Framework - Injecting a Map

In this example we will try to populate employees map using spring's setter injection.

Bean Configuration File

<?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="myCompany" class="com.examples.spring.Company">
    <property name="name" value="myWorld" />
    <property name="employees">
      <map>
        <entry>
          <key>
            <value>pankaj</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Pankaj Tiwari" />
          </bean>
        </entry>
        <entry>
          <key>
            <value>paresh</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Paresh Tiwari" />
          </bean>
        </entry>
        <entry>
          <key>
            <value>ankit</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Ankit Rawat" />
          </bean>
        </entry>
      </map>
    </property>
  </bean>
</beans>

The Company class showing changes for setting the map property
package com.examples.spring;

import java.util.Map;

public class Company {
 private String companyName;
 private Map<String, Employee> employees;

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

 public String getName() {
  return companyName;
 }

 public void setEmployees(Map<String, Employee> employees) {
  this.employees = employees;
 }

 public Map<String, Employee> getEmployees() {
  return employees;
 }
}

The Employee bean class
package com.examples.spring;

public class Employee {
 private String empName;

 public Employee(String name) {
  this.empName = name;
 }

 public String getName() {
  return empName;
 }
}

The Junit test program for testing the above
package com.examples.spring;

import java.util.Map;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * File: AppTest.java
 */
public class AppTest {

 ApplicationContext ctx = null;

 @Before
 public void setup() {
  ctx = new ClassPathXmlApplicationContext("beans.xml");
 }

 @After
 public void cleanup() {
 }

 @Test
 public void testPropertyInjection() {
  Company myCompany = (Company) ctx.getBean("myCompany");
  Assert.assertEquals(myCompany.getName(), "myWorld");
  Assert.assertEquals(myCompany.getEmployees().size(), 3);
  Map<String, Employee> employees = myCompany.getEmployees();
  for (Map.Entry<String, Employee> entry : employees.entrySet()) {
   System.out.println(entry.getKey() + ":" + entry.getValue().getName());
  }
 }
}

No comments :

Post a Comment