Spring Supports 4 types of Collections
1. Set
2. List
3. Map
4. Properties
If you want use other collections we have to configure our self-using <util/> tag
By using Collections we can inject String data and non-String data (Objects)
1. Set
· By using set we can pass String & Object data
· In set we must pass Unique data , no duplicates are allowed
· Here Dependent Object will create first rather than bean Object
· We use <set/> tag for injecting set data
· If data is String type we use <value/> tag under <set >
· If data is Object type, we use <ref/> tag under <set >
Syntax
<property name="setData">
<set>
<value>Set-HYD</value>
<value>Set-VZG</value>
<value>Set-BAN</value>
</set>
</property>
2. List
· In List we can pass duplicates
· Here Dependent Object will create first rather than bean Object
· We use <list/> tag for injecting list data
· If data is String type we use <value/> tag under < list >
· If data is Object type, we use <ref/> tag under < list>
Syntax
<property name="listData">
<list>
<value>List-IND</value>
<value>List-PAK</value>
<value>List-ZIM</value>
</list>
</property>
3. Map
· Map pass they data in the form of <key, value > pair
· We use <map/> tag for injecting map data
· If data is String type we use <entry key=” ” value=” ”/> tag under< map>
· If data is Object type, we use <entry key-ref=” ” value-ref=” ”/> tag under < map >
Syntax
<property name="mapData">
<map>
<entry key="1" value="Map-SATYA" />
<entry key="2" value="Map-RAJESH" />
<entry key="3" value="Map-JOHNNY" />
</map>
</property>
4. Properties
· Properties pass they data in the form of <key= value > pair
· We use <props/> tag for injecting Properties data
· we use <prop key=" ">value</prop> tag under < props >
Syntax
<property name="propData">
<props>
<prop key="1">Prop-MP</prop>
<prop key="2">Prop-UP</prop>
<prop key="3">Prop-AP</prop>
</props>
</property>
Example 1: Collections with String Data
//StringBean.java
package cbean;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class StringBean {
private Set<String> setData;
private List<String> listData;
private Map mapData;
private Properties propData;
public Set<String> getSetData() {
return setData;
}
public void setSetData(Set<String> setData) {
this.setData = setData;
}
public List<String> getListData() {
return listData;
}
public void setListData(List<String> listData) {
this.listData = listData;
}
public Map getMapData() {
return mapData;
}
public void setMapData(Map mapData) {
this.mapData = mapData;
}
public Properties getPropData() {
return propData;
}
public void setPropData(Properties propData) {
this.propData = propData;
}
}
|
//CollectionMain.java
package main;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cbean.StringBean;
public class CollectionMain {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("res/StringColl.xml");
StringBean bean = (StringBean) factory.getBean("ob");
System.out.println("List Data : "+bean.getListData().toString());
System.out.println("Set Data : "+bean.getSetData().toString());
System.out.println("Map Data : "+bean.getMapData().toString());
System.out.println("Prop Data : "+bean.getPropData().toString());
}
}
|
StringColl.xml
<beans>
<bean id="ob" class="cbean.StringBean">
<property name="setData">
<set>
<value>Set-HYD</value>
<value>Set-VZG</value>
<value>Set-BAN</value>
</set>
</property>
<property name="listData">
<list>
<value>List-IND</value>
<value>List-PAK</value>
<value>List-ZIM</value>
</list>
</property>
<property name="mapData">
<map>
<entry key="1" value="Map-SATYA" />
<entry key="2" value="Map-RAJESH" />
<entry key="3" value="Map-JOHNNY" />
</map>
</property>
<property name="propData">
<props>
<prop key="1">Prop-MP</prop>
<prop key="2">Prop-UP</prop>
<prop key="3">Prop-AP</prop>
</props>
</property>
</bean>
</beans>
|
Output
log4j:WARN Please initialize the log4j system properly.
List Data : [List-IND, List-PAK, List-ZIM]
Set Data : [Set-HYD, Set-VZG, Set-BAN]
Map Data : {1=Map-SATYA, 2=Map-RAJESH, 3=Map-JOHNNY}
Prop Data : {3=Prop-AP, 2=Prop-UP, 1=Prop-MP}
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=false
|