Collections Injection Example:
Bean.java
package bean;
import java.util.*;
public class Bean {
private Set<String> setData;
private List<String> listData;
private Map mapData;
private Properties propData;
public Bean(Set set, List list, Map map, Properties properties) {
// TODO Auto-generated constructor stub
this.setData = set;
this.listData = list;
this.mapData = map;
this.propData = properties;
}
public void getData(){
System.out.println(setData);
System.out.println(listData);
System.out.println(mapData);
System.out.println(propData);
}
}
Main.java
package main;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Bean;
public class Main {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("res/s.xml");
Bean bean = (Bean) factory.getBean("ob");
bean.getData();
}
}
Res/s.xml
<beans>
<bean id="ob" class="bean.Bean">
<constructor-arg index="0">
<set>
<value>Set-HYD</value>
<value>Set-VZG</value>
<value>Set-BAN</value>
</set>
</constructor-arg>
<constructor-arg index="1">
<list>
<value>List-IND</value>
<value>List-PAK</value>
<value>List-ZIM</value>
</list>
</constructor-arg>
<constructor-arg index="2">
<map>
<entry key="1" value="Map-SATYA" />
<entry key="2" value="Map-RAJESH" />
<entry key="3" value="Map-JOHNNY" />
</map>
</constructor-arg>
<constructor-arg index="3">
<props>
<prop key="1">Prop-MP</prop>
<prop key="2">Prop-UP</prop>
<prop key="3">Prop-AP</prop>
</props>
</constructor-arg>
</bean>
</beans>
Output
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
[Set-HYD, Set-VZG, Set-BAN]
[List-IND, List-PAK, List-ZIM]
{1=Map-SATYA, 2=Map-RAJESH, 3=Map-JOHNNY}
{3=Prop-AP, 2=Prop-UP, 1=Prop-MP}
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=false