上一节中,我们学习了bean的生命周期,可能在我们实际工作中并不能全部用到。但是知识嘛,了解的越多,能够解决的问题也就越多,我们也就越高兴。(好吧,这不是我的真心话T_T)
在这一节中,我们主要学习一下bean的装配方式。
在这里,我只介绍常用的集合的装配方式:组合、list、set、map
下面是Employee.java 的代码
package rogue.collection;
public class Employee {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
下面是Department.java 的代码
package rogue.collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Department {
private String name;
private String [] epmName;
private List<Employee> employees;
private Set<Employee> eSet;
private Map<String, Employee> eMap;
public Map<String, Employee> geteMap() {
return eMap;
}
public void seteMap(Map<String, Employee> eMap) {
this.eMap = eMap;
}
public Set<Employee> geteSet() {
return eSet;
}
public void seteSet(Set<Employee> eSet) {
this.eSet = eSet;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEpmName() {
return epmName;
}
public void setEpmName(String[] epmName) {
this.epmName = epmName;
}
}
下面是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="rogue.collection.Department">
<property name="name">
<value>统战部</value>
</property>
<!-- 给数组注入值 -->
<property name="epmName">
<list>
<value>星矢</value>
<value>龙马</value>
<value>拓海</value>
</list>
</property>
<!-- 给list注入值 -->
<property name="employees">
<list>
<ref bean= "emp1"/>
<ref bean= "emp2"/>
<ref bean= "emp3"/>
</list>
</property>
<!-- 给set注入值 -->
<property name="eSet">
<list>
<ref bean= "emp1"/>
<ref bean= "emp2"/>
<ref bean= "emp3"/>
</list>
<!-- 给Map注入值-->
<property name="eMap">
<map>
<entry key="1" value-ref="emp1"/>
<entry key="2" value-ref="emp2"/>
<entry key="3" value-ref="emp3"/>
</map>
可以看到map是通过entry来配置的。这里的key有两种方式,一种是“key”,另一种是“key-ref”,他的道理和value是一样的,key也是可以引用其他的bean的。
</property>
</bean>
<bean id="emp1" class="rogue.collection.Employee">
<property name="name" value="星矢" />
<property name="age" value="12" />
</bean>
<bean id="emp2" class="rogue.collection.Employee">
<property name="name" value="龙马" />
<property name="age" value="19" />
</bean>
<bean id="emp3" class="rogue.collection.Employee">
<property name="name" value="拓海" />
<property name="age" value="21" />
</bean>
</beans>
下面是测试main函数的代码
package rogue.collection;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCollection {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("rogue/collection/beans.xml");
这里可以看到,文件的路径和以前的相比发生了变化,注意书写格式
Department department = (Department) ac.getBean("departMent");
System.out.println("********通过数组集合来配置bean**********");
for (String eString : department.getEpmName() ) {
System.out.println("战士有:"+eString);
}
System.out.println("********通过list集合来配置bean**********");
for (Employee employee : department.getEmployees()) {
System.out.println("名字是:"+employee.getName()+"----年龄是:"+employee.getAge());
}
System.out.println("********通过Set集合来配置bean**********");
for (Employee employee : department.geteSet()) {
System.out.println("名字是:"+employee.getName()+"----年龄是:"+employee.getAge());
}
System.out.println("********通过Map集合来配置bean**********");
for (Entry<String, Employee> employee : department.geteMap().entrySet()) {
System.out.println("名字是:"+employee.getValue().getName()+"-----年龄是:"+employee.getValue().getAge()+"-----他的id是"+employee.getKey());
}
}
}