集合注入重要是对数组、List、Set、map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式):
1.在工程中新建一个Department类,该类包含在com.LHB.collection包当中
package com.LHB.collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Department {
private String name;
private String[] empName;
private List<Employee> empList; //List集合
private Set<Employee> empSets; //Set集合
private Map<String,Employee> empMap; //map集合
private Properties pp; //Properties的使用 public Properties getPp() {
return pp;
}
public void setPp(Properties pp) {
this.pp = pp;
}
public Map<String, Employee> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<String, Employee> empMap) {
this.empMap = empMap;
}
public Set<Employee> getEmpSets() {
return empSets;
}
public void setEmpSets(Set<Employee> empSets) {
this.empSets = empSets;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
}
2.继续在包中创建Employee类
package com.LHB.collection;
public class Employee {
private String name;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
3.创建applicationContext.xml配置文件,配置重点在数组,List,Set,Map,propertes装载值的环节
<?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 id="department" class="com.LHB.collection.Department">
<property name="name" value="财务部门" />
<!-- 给数组注入值 -->
<property name="empName">
<list>
<value>小米</value>
<value>小明</value>
<value>小四</value>
</list>
</property> <!-- 给list注入值 可以有相同的多个对象 -->
<property name="empList">
<list>
<ref bean="emp1" />
<ref bean="emp2"/>
</list>
</property>
<!-- 给set注入值 不能有相同的对象 -->
<property name="empSets">
<set>
<ref bean="emp1" />
<ref bean="emp2"/>
</set>
</property> <!-- 给map注入值 只要map中的key值不一样就可以装配value -->
<property name="empMap">
<map>
<entry key="1" value-ref="emp1" />
<entry key="2" value-ref="emp2" />
</map>
</property> <!-- 给属性集合配置 -->
<property name="pp">
<props>
<prop key="pp1">hello</prop>
<prop key="pp2">world</prop>
</props>
</property>
</bean>
<bean id="emp1" class="com.LHB.collection.Employee">
<property name="name">
<value>北京</value>
</property>
</bean>
<bean id="emp2" class="com.LHB.collection.Employee">
<property name="name">
<value>天津</value>
</property>
</bean> </beans>
4.继续在该包中新建App1.java测试类
package com.LHB.collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 { public static void main(String[] args) {
// TODO Auto-generated method stub //通过类路径应用上下文获取配置文件applicationContext.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("com/LHB/collection/applicationContext.xml");
//通过getBean()获取到applicationContext.xml文件中Bean对象
Department dm = (Department) ac.getBean("department");
System.out.println(dm.getName());
//取出数组中的值
for(String emName : dm.getEmpName()){
System.out.println(emName);
} System.out.println("*********通过List集合取出数据******");
for(Employee e : dm.getEmpList()){
System.out.println("name = "+ e.getName());
} System.out.println("*********通过Set集合取出数据******");
for(Employee e : dm.getEmpSets()){
System.out.println("name = "+ e.getName());
} System.out.println("*********通过Map集合取出数据(迭代器)******");
//迭代器
Map<String,Employee> empMap = dm.getEmpMap();
Iterator it = empMap.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
Employee emp = empMap.get(key);
System.out.println("key = " + key + " " + emp.getName());
}
System.out.println("*********通过Map集合取出数据(Emtry简洁法)******");
//简洁方法
for(Entry<String,Employee> entry : dm.getEmpMap().entrySet()){ System.out.println(entry.getKey()+ " " + entry.getValue().getName());
} System.out.println("*********通过Propertis取出数据(通过Entry对象取)******");
Properties pp = dm.getPp();
for(Entry<Object,Object> entry : pp.entrySet()){
System.out.println(entry.getKey().toString() + ", "+ entry.getValue().toString());
}
System.out.println("*********通过Propertis取出数据(通过Enumeration对象取)******");
Enumeration en = pp.keys();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
System.out.println(key + " " + pp.getProperty(key));
}
}
}
运行结果如下: