使用的是set注入:
package com.chen;
import java.util.*;
public class Stu {
//1.数组类型
private String[] courses;
//2.list集合类型属性
private List<String> list;
//3.map集合类型属性
Map<String,Object> map;
//4.set集合类型的 属性
private Set<String> set;
public List<Course> courseList;
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void show(){
for (String cours : courses) {
System.out.println("数组==》"+cours);
}
for (String s : list) {
System.out.println("list==>"+s);
}
for (String s : set) {
System.out.println("set==>"+s);
}
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
Object o = map.get(next);
System.out.println(next+":"+o);
}
for (Course course : courseList) {
System.out.println("courseList==>"+course);
}
}
}
这是xml配置,里面分别展示了几种类型的value值的写法
<?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="com.chen.Stu">
<property name="courses">
<array>
<value>java课程</value>
<value>英语课程</value>
<value>数学课程</value>
<value>体育</value>
</array>
</property>
<property name="list">
<list>
<value>香蕉</value>
<value>苹果</value>
<value>橘子</value>
</list>
</property>
<property name="set">
<set>
<value>足球</value>
<value>篮球</value>
<value>羽毛球</value>
</set>
</property>
<property name="map">
<map>
<entry key="英语" value="89"></entry>
<entry key="数学" value="190"></entry>
<entry key="身高" value="178"></entry>
</map>
</property>
<!--注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1" ></ref>
<ref bean="course2" ></ref>
<ref bean="course3" ></ref>
</list>
</property>
</bean>
<bean id="course1" class="com.chen.Course">
<property name="courname" value="化学"></property>
</bean>
<bean id="course2" class="com.chen.Course">
<property name="courname" value="政治"></property>
</bean>
<bean id="course3" class="com.chen.Course">
<property name="courname" value="物理"></property>
</bean>
</beans>