tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Map Injection

Map Injection 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. The following example shows map injection.

File Name  :  
/SpringCore001/conf/collections/map_inj.xml 
01<?xml version="1.0" encoding="UTF-8"?>
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04       xsi:schemaLocation="http://www.springframework.org/schema/beans
06 
07    <bean id="complexBean" class="com.bethecoder.tutorials.spring3.basic.ComplexBean">
08        <property name="simpleMap">
09          <map>
10              <entry key="emp2" value="Employee2"/>
11              <entry key ="emp1" value-ref="emp1"/>
12              <entry key ="emp3" >
13                <null></null>
14              </entry>
15          </map>
16        </property>
17    </bean>
18 
19    <bean id="emp1" class="com.bethecoder.tutorials.spring3.basic.Employee">
20        <property name="id" value="1" />
21        <property name="name" value="ABC" />
22        <property name="salary" value="90000" />
23    </bean>
24     
25</beans>

File Name  :  
com/bethecoder/tutorials/spring3/basic/ComplexBean.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class ComplexBean {

  private java.util.Properties simpleProps;
  private java.util.List simpleList;
  private java.util.Map simpleMap;
  private java.util.Set simpleSet;
  
  public java.util.Properties getSimpleProps() {
    return simpleProps;
  }
  
  public void setSimpleProps(java.util.Properties simpleProps) {
    this.simpleProps = simpleProps;
  }
  
  public java.util.List getSimpleList() {
    return simpleList;
  }
  
  public void setSimpleList(java.util.List simpleList) {
    this.simpleList = simpleList;
  }
  
  public java.util.Map getSimpleMap() {
    return simpleMap;
  }
  
  public void setSimpleMap(java.util.Map simpleMap) {
    this.simpleMap = simpleMap;
  }
  
  public java.util.Set getSimpleSet() {
    return simpleSet;
  }
  
  public void setSimpleSet(java.util.Set simpleSet) {
    this.simpleSet = simpleSet;
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/basic/Employee.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class Employee {
  
  private int id;
  private String name;
  private double salary;
  
  public Employee() {
    super();
    this.id = -1;
    this.name = "Unknown";
    this.salary = 0;
  }
  
  public Employee(int id, String name) {
    super();
    this.id = id;
    this.name = name;
    this.salary = 20000;
  }
  
  public Employee(int id, String name, double salary) {
    super();
    this.id = id;
    this.name = name;
    this.salary = salary;
  }

  public int getId() {
    return id;
  }
  
  public void setId(int id) {
    this.id = id;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public double getSalary() {
    return salary;
  }
  
  public void setSalary(double salary) {
    this.salary = salary;
  }
  
  public String toString() {
    return "Employee[" + id + ", " + name + ", " + salary + "]";
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/collections/MapInjection.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.spring3.tests.collections;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.bethecoder.tutorials.spring3.basic.ComplexBean;

public class MapInjection {

  /**
   @param args
   */
  public static void main(String[] args) {
    XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("map_inj.xml"));

    ComplexBean complexBean = (ComplexBeanfactory.getBean("complexBean");
    System.out.println(complexBean.getSimpleMap());

  }

}
   

It gives the following output,
{emp2=Employee2, emp1=Employee[1, ABC, 90000.0], emp3=null}



 
  


  
bl  br