IOC 操作 Bean 管理(xml 注入集合属性)
1、注入数组类型属性
2、注入 List 集合类型属性
3、注入 Map 集合类型属性
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
package collection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
public void setCourses(String[] courses) {
this.courses = courses;
}
private String[] courses;
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
private Map<String,String> maps;
public void setSets(Set<String> sets) {
this.sets = sets;
}
private Set<String> sets;
public void test(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
}
}
配置类的编写
<?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.xsd">
<bean id="stu" class="collection.Stu">
<!-- 数据类型的注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>数据结构</value>
</array>
</property>
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<property name="maps">
<map>
<entry key="Java" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
</beans>
测试类的编写,执行
@Test
public void test6(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");
Stu stu=context.getBean("stu",Stu.class);
stu.test();
}