Spring 中集合注入:
当 javabean 的属性中有 List 类型或者数组时,使用 <list> 注入值
当 属性中有 Set 类型时使用 <set> 标签注入值
当属性中有 Map 类型时使用 <map>标签注入值
List 类型和 数组 也可以通过<set>标签注入,当会自动去重
实例代码:
applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<bean name="teacher" class="com.spring.demo.Teacher">
<!-- setter方法注入 -->
<property name="name" value="离开一"></property>
<property name="teacherNumber" value="12"></property>
<property name="course" ref="course"></property>
</bean>
<bean name="student" class="com.spring.demo.Student">
<!-- 构造器注入 —— 通过参数类型注入 -->
<!--<constructor-arg type="java.lang.String"><value><![CDATA[\\>张三]]></value></constructor-arg>
<constructor-arg type="java.lang.String" value="456"></constructor-arg>
<constructor-arg type="int" value="18"></constructor-arg>
<constructor-arg type="java.lang.String" value="1997-10-04"></constructor-arg>
<constructor-arg type="com.spring.demo.Teacher" ref="teacher"></constructor-arg>
-->
<property name="name" value="李伟"></property>
<property name="studentNumber" value="20162430211"></property>
<property name="age" value="20"></property>
<property name="birthday" value="1997-10-4"></property>
<property name="teacher" ref="teacher"></property>
<property name="courseList" >
<list>
<!-- List中存储的是对象 -->
<ref bean="course"></ref>
<ref bean="course"></ref>
<ref bean="course"></ref>
<ref bean="course"></ref>
</list>
</property>
<property name="teacherMap">
<map>
<entry key="王老师" value="数据结构"></entry>
<entry key="李老师" value="java"></entry>
<entry key="张老师" value="算法分析"></entry>
<entry key="孙老师" value="C语言"></entry>
</map>
</property>
</bean>
<bean name="course" class="com.spring.demo.Course">
<!--构造器注入 —— 通过索引注入 -->
<constructor-arg index="0" value="数据结构"></constructor-arg>
<constructor-arg index="1" value="123"></constructor-arg>
</bean>
</beans>
Student 类
package com.spring.demo;
import java.util.List;
import java.util.Map;
public class Student {
private String name;
private String studentNumber;
private int age;
private String birthday;
private Teacher teacher;
private List<Course> courseList;
private Map<String,String> teacherMap;
public Student(){}
public Student(String name, String studentNumber, int age, String birthday, Teacher teacher) {
super();
this.name = name;
this.studentNumber = studentNumber;
this.age = age;
this.birthday = birthday;
this.teacher = teacher;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
/**
* @return the teacher
*/
public Teacher getTeacher() {
return teacher;
}
/**
* @param teacher the teacher to set
*/
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public Map<String, String> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, String> teacherMap) {
this.teacherMap = teacherMap;
}
@Override
public String toString() {
return "Student [name=" + name + ", studentNumber=" + studentNumber + ", age=" + age + ", birthday=" + birthday
+ ", teacher=" + teacher + ", courseList=" + courseList + ", teacherMap=" + teacherMap + "]";
}
}
beanDemo 类:
package com.spring.demo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanDemo {
@Test
public void beanDemo(){
//获取SpringIOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("config/applicationContext.xml");
//获取Course对象
/*Course course = ioc.getBean("course",Course.class);
System.out.println(course.toString());*/
// Teacher teacher = ioc.getBean("teacher",Teacher.class);
// System.out.println(teacher.toString());
Student student = ioc.getBean("student",Student.class);
System.out.println(student.toString());
}
}