今天继续上次的iocbean管理的xml注入方式,今天是xmlv注入集合属性
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.xsd"> <!-- 集合类型属性注入--> <bean id="stu" class="collectiontype.Stu"> <!-- 数组类型属性注入--> <property name="courses"> <array> <value>java课程</value> <value>spring课程</value> </array> </property> <!-- list属性注入--> <property name="lsit"> <list> <value>***</value> <value>******</value> </list> </property> <!-- map属性注入--> <property name="maps"> <map> <entry key="Java" value="xxx"></entry> <entry key="python" value="yyy"></entry> </map> </property> <!-- set属性注入--> <property name="sets"> <set> <value>redis</value> <value>mysql</value> </set> </property> <!-- list对象集合注入--> <property name="courseList"> <list> <ref bean="course1"></ref> <ref bean="course2"></ref> </list> </property> </bean> <!-- 创建多个course对象--> <bean id="course1" class="collectiontype.Course"> <property name="cname" value="Spring5"></property> </bean> <bean id="course2" class="collectiontype.Course"> <property name="cname" value="Spring6"></property> </bean> </beans>
类:
package collectiontype; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; public class Stu { //数组类型属性 private String[] courses; //list类型属性 private List<String> lsit; //map集合类型属性 private Map<String,String> maps; //set集合类型属性 private Set<String> sets; //学生所学多门课程 private List<Course> courseList; public void setCourses(String[] courses) { this.courses = courses; } public void setLsit(List<String> lsit) { this.lsit = lsit; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public void setSets(Set<String> sets) { this.sets = sets; } public void setCourseList(List<Course> courseList) { this.courseList = courseList; } public void print(){ System.out.println(Arrays.toString(courses)); System.out.println(lsit); System.out.println(maps); System.out.println(sets); System.out.println(courseList); } }
package collectiontype; public class Course { private String cname; public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "Course{" + "cname='" + cname + '\'' + '}'; } }
package collectiontype; import java.util.List; public class Book { private List<String> booklist; public void setBooklist(List<String> booklist) { this.booklist = booklist; } public void test(){ System.out.println(booklist); } }