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

Typed 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 typed map injection.

File Name  :  
/SpringCore001/conf/typed_collections/typed_map_inj.xml 

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

public class ComplexTypedBean {

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

File Name  :  
com/bethecoder/tutorials/spring3/tests/typed_collections/TypedMapInjection.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.typed_collections;

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

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

public class TypedMapInjection {

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

    ComplexTypedBean complexBean = (ComplexTypedBeanfactory.getBean("complexBean");
    System.out.println(complexBean.getSimpleMap());

  }

}
   

It gives the following output,
{one=1111.11, two=2222.22, three=3333.33}



 
  


  
bl  br