beans.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="department" class="com.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注值 -->
<property name="empMaps">
<map>
<entry key="1" value-ref="emp1"/>
<entry key="2" value-ref="emp2"/>
</map>
</property>
</bean>
<!-- 给list注入值 -->
<bean id="emp1" class="com.collection.Employee">
<property name="name" value="北京"/>
<property name="id" value="1"/>
</bean>
<bean id="emp2" class="com.collection.Employee">
<property name="name" value="天津"/>
<property name="id" value="2"/>
</bean>
</beans>
Department.java:
package com.collection;
import java.util.*;
public class Department {
private String name;
private String []empName;
private List<Employee> emplist;
private Set<Employee> empsets;
private Map<String,Employee> empMaps;
public Map<String, Employee> getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
}
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;
}
}
Employee.java:
package com.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;
}
}
test.java:
package com.collection;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("com/collection/beans.xml");
Department a=(Department) ac.getBean("department");
String []empName=a.getEmpName();
for(int q=0;q<empName.length;q++){
System.out.println(empName[q]);
}
System.out.println("*****************************************");
for(Employee e:a.getEmplist()){
System.out.println(e.getName());
}
System.out.println("*****************************************");
for(Employee e:a.getEmpsets() ){
System.out.println(e.getName());
}
System.out.println("*****************************************");
for(Entry<String,Employee> e:a.getEmpMaps().entrySet() ){
System.out.println(e.getKey()+" "+e.getValue().getName());
}
}
}