We have previously seen how to deal with List, Set and Map type
attributes inside a bean class. Further let us venture into having
concrete List collection (ArrayList and LinkedList), Set
collection(HashSet and TreeSet)Â Map collection(HashMap and TreeMap)
File : ClassABC.java
File : Beans.xml
File : MainClass.java
Step 1 : Create the POJO
File : ClassABC.java
package com.simpleCodeStuffs; import java.util.List; import java.util.Map; import java.util.Set; public class ClassABC { private List listVariable; private Map mapVariable; private Set setVariable; public List getListVariable() { return listVariable; } public void setListVariable(List listVariable) { this.listVariable = listVariable; } public Map getMapVariable() { return mapVariable; } public void setMapVariable(Map mapVariable) { this.mapVariable = mapVariable; } public Set getSetVariable() { return setVariable; } public void setSetVariable(Set setVariable) { this.setVariable = setVariable; } public String toString() { return ("listVariable t" + listVariable + "nsetVariable t" + setVariable + "nmapVariable t" + mapVariable); } }File : Amount.java
package com.simpleCodeStuffs; public class Amount { private String val; public String getVal() { return val; } public void setVal(String val) { this.val = val; } public String toString() { return ("amount bean val: " + val); } }
Step 2 : Provide the configuration metadata in the xml file
To support the concrete classes of List, Map and Set, it is essential to include util schema in the Beans.xml file.Else, you will end up getting a
SAXParseException because of it
File : Beans.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" xmlns:
util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="abc" class="com.simpleCodeStuffs.ClassABC"> <property name="listVariable"> <util:list list-class="java.util.ArrayList"> <value>1</value> <value>ArrayList</value> <ref bean="amount" /> </util:list> </property> <property name="setVariable"> <util:set set-class="java.util.HashSet"> <value>2</value> <value>HashSet</value> <ref bean="amount" /> </util:set> </property> <property name="mapVariable"> <util:map map-class="java.util.HashMap"> <entry key="Key1" value="3" /> <entry key="Key2" value="HashMap" /> <entry key="Key3" value-ref="amount" /> </util:map> </property> </bean> <bean id="amount" class="com.simpleCodeStuffs.Amount"> <property name="val" value="simpleCodeStuffs" /> </bean> </beans>
Step 3 :
File : MainClass.java
package com.simpleCodeStuffs; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainClass { public static void main(String[] args) { ApplicationContext context = new
ClassPathXmlApplicationContext("Beans.xml");
ClassABC abc = (ClassABC) context.getBean("abc"); System.out.println(abc); } }