Working with Set,List,Map and Properties as attributes

![]() |
|
We have seen examples of having a classA as one of the attributes of another classB. We also have cases wherein the attributes of the class are a List of another object(classA).
Example File : Bill
public class Bill { private List<ABC> abc; }
File : ABC
Public class ABC{ Private double amount; Private double discount; }
For such cases, the value for the List or any other similar collection needs to be injected through framework, which involves a slight syntactical change.
Step 1 :
Create the POJO which is going to be one of the attributes of another class.File : Amount.java
package com.simpleCodeStuffs; public class Amount { private double bill; public double getBill() { return bill; } public void setBill(double bill) { this.bill = bill; } @Override public String toString(){ return("amount "+bill); } }
Step 2 :
CollectionBean is a class, which has a list,set,map,properties of another Object as its attributes. Just to enhance the understanding, we take it that CollectionBean may have a List of any Object as its attributes. Hence the attributes are defined as List of Object and likewise.
File : CollectionBean.java
package com.simpleCodeStuffs; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionBean { private List<String> stringListVariable; private List<Object> listVariable; private Set<Object> setVariable; private Map<Object, Object> mapVariable; private Properties propVariable; public List<String> getStringListVariable() { return stringListVariable; } public void setStringListVariable(List<String> stringListVariable) { this.stringListVariable = stringListVariable; } public List<Object> getListVariable() { return listVariable; } public void setListVariable(List<Object> listVariable) { this.listVariable = listVariable; } public Set<Object> getSetVariable() { return setVariable; } public void setSetVariable(Set<Object> setVariable) { this.setVariable = setVariable; } public Map<Object, Object> getMapVariable() { return mapVariable; } public void setMapVariable(Map<Object, Object> mapVariable) { this.mapVariable = mapVariable; } public Properties getPropVariable() { return propVariable; } public void setPropVariable(Properties propVariable) { this.propVariable = propVariable; } }
Step 3 : Main class to print the details of the attributes
File : MainClass.java
package com.simpleCodeStuffs; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.simpleCodeStuffs.Amount; import com.simpleCodeStuffs.CollectionBean; public class MainClass { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); CollectionBean bean=(CollectionBean)context.getBean("testBean"); Amount amt=(Amount)context.getBean("amount"); System.out.println("List of String:t"+bean.getStringListVariable()); System.out.println("List :t"+bean.getListVariable()); System.out.println("set :t"+bean.getSetVariable()); System.out.println("Map :t"+bean.getMapVariable()); System.out.println("Properties :t"+bean.getPropVariable()); } }
Step 4 :
This is the main change. For each of the collection variables, the way in which the value is injected in the configuration metadata has a syntax change.
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="testBean" class="com.simpleCodeStuffs.CollectionBean"> <property name="stringListVariable"> <list> <value>A Simple</value> <value>List</value> <value>Of</value> <value>String</value> </list> </property> <property name="listVariable"> <list> <value>1</value> <value>List of Objects</value> <ref bean="amount" /> <bean class="com.simpleCodeStuffs.Amount"> <property name="bill"> <value>45.9</value> </property> </bean> </list> </property> <property name="setVariable"> <set> <value>2</value> <value>Set of Objects</value> <ref bean="amount" /> <bean class="com.simpleCodeStuffs.Amount"> <property name="bill"> <value>09.5</value> </property> </bean> </set> </property> <property name="mapVariable"> <map> <entry key="a" value="3" /> <entry key="b" value="Map of Objects" /> <entry key="c" value-ref="amount" /> <entry key="d"> <bean class="com.simpleCodeStuffs.Amount"> <property name="bill"> <value>76.5</value> </property> </bean> </entry> </map> </property> <property name="propVariable"> <props> <prop key="a">4</prop> <prop key="b">properties</prop> </props> </property> </bean> <bean id="amount" class="com.simpleCodeStuffs.Amount"> <property name="bill"> <value>56.9</value> </property> </bean> </beans>
The first example shown, âstringListVariableâ is simply to state the means by which multiple values of a simple Data type can be injected into the list. The other variables take values of type Object. Hence, first an Integer, then a String is inserted.
The third one is to show how a reference to another bean can be inserted as a value to the list/set/map. The fourth one shows how an inner bean can be defined and the value inserted to the list/set/map. Note here that, while defining an inner bean, there is no need to mention the beanId as it has limited scope to only the outer bean which defines it.
Step 5 : Run the application. The output appears as :-

![]() |
|