Spring 集合注入
2017-03-28 本文已影响0人
Draper
在 Spring 中注入 List
, Set
, Map
,我们该怎么进行注入呢?,这篇文章接下来讲的 就是这个。 如果你对注入不太了解的话,可以看这篇文章。
Spring 依赖注入(基础)
List
首先我们要有个 Student
类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void study() {
System.out.println(name + "在学习");
}
public String getName() {
return name;
}
}
接下来是管理 Student
类的 School
类
public class School {
private List<Student> studentList;
public School(List<Student> studentList) {
this.studentList = studentList;
}
public void study() {
for (Student student : studentList) {
student.study();
}
}
}
接下来是 bean.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">
<beans>
<bean id="school" class="com.draper.School">
<constructor-arg>
<list>
<bean id="stu1" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="stu2" class="com.draper.Student">
<constructor-arg index="0">
<value>小贝</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
</beans>
亦或是应用我们上篇文章学的 在 bean
中注入 bean
<?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">
<beans>
<bean id="stu1" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="stu2" class="com.draper.Student">
<constructor-arg index="0">
<value>小贝</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="school" class="com.draper.School">
<constructor-arg>
<list>
<ref bean="stu1"/>
<ref bean="stu2"/>
</list>
</constructor-arg>
</bean>
</beans>
</beans>
对比我们前面学的总结下
<list>
的标签其实他已经给你封装好了,<set>
, <map>
也是一样,只不过细节上有些不同, 而引入另外一个 bean
还是用了 <ref>
让我们来测试一下
public class TestApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
School school = (School) context.getBean("school");
school.study();
}
}
Set
Set
就是把标签换成 <set>
,再修改下类就可以了
Map
为了方便演示,略微修改 Student
类
public class School {
private Map<String, Student> studentMap;
public School(Map<String, Student> studentMap) {
this.studentMap = studentMap;
}
public void study(){
Set<String> keySet = studentMap.keySet();
for (String s : keySet) {
studentMap.get(s).study();
}
}
}
修改 bean.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">
<beans>
<bean id="stu1" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="stu2" class="com.draper.Student">
<constructor-arg index="0">
<value>小贝</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="school" class="com.draper.School">
<constructor-arg>
<map>
<entry key="stu1">
<ref bean="stu1"/>
</entry>
<entry key="stu2">
<ref bean="stu2"/>
</entry>
</map>
</constructor-arg>
</bean>
</beans>
</beans>
测试代码没有改动
的确有点神奇哈