Typed List 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 list injection.
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;
}
}
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 TypedListInjection {
/**
* @param args
*/
public static void main ( String [] args ) {
XmlBeanFactory factory = new XmlBeanFactory (
new ClassPathResource ( "typed_list_inj.xml" )) ;
ComplexTypedBean complexBean = ( ComplexTypedBean ) factory.getBean ( "complexBean" ) ;
System.out.println ( complexBean.getSimpleList ()) ;
}
}
It gives the following output,
[12.456, 45.123, 68.456]