在 Spring 中注入 List
, Set
, Map
土思,我們該怎么進行注入呢袋狞?,這篇文章接下來講的 就是這個凄贩。 如果你對注入不太了解的話誓军,可以看這篇文章。
Spring 依賴注入(基礎(chǔ))
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 + "在學(xué)習(xí)");
}
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>
亦或是應(yīng)用我們上篇文章學(xué)的 在 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>
對比我們前面學(xué)的總結(jié)下
<list>
的標簽其實他已經(jīng)給你封裝好了疲扎,<set>
, <map>
也是一樣昵时,只不過細節(jié)上有些不同, 而引入另外一個 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>
測試代碼沒有改動
的確有點神奇哈