#集合类型(复杂类型)的属性注入
集合类型(复杂类型)的属性注入,包括arrs、map、list、set、properties五大类型的属性注入
##示例代码如下:
注:本示例用idea编写
###CollectionBean.java类
public class CollectionBean {
private String[] arrs;
private Map map;
private List list;
private Set set;
private Properties properties;
public String[] getArrs() {
return arrs;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "CollectionBean{" +
"arrs=" + Arrays.toString(arrs) +
", map=" + map +
", list=" + list +
", set=" + set +
", properties=" + properties +
'}';
}
}
###配置文件ApplicationContext.xml
<bean name="collectionBean" class="com.ioc.demo5.CollectionBean">
<!--数组类型-->
<property name="arrs">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--list类型-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王六</value>
</list>
</property>
<!--map类型-->
<property name="map">
<map>
<entry key="a" value="老大"></entry>
<entry key="b" value="老二"></entry>
<entry key="c" value="老三"></entry>
</map>
</property>
<!--properties类型-->
<property name="properties">
<props>
<prop key="username" >root</prop>
<prop key="password" >123456</prop>
</props>
</property>
<!--set类型-->
<property name="set">
<set>
<value>eee</value>
<value>fff</value>
<value>ttt</value>
</set>
</property>
</bean>
###SpringDemo5.java测试类
public class SpringDemo5 {
@Test
public void demo1(){
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("ApplicationContext.xml");
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
}
###测试结果:如图