上面一章我们学习了bean配置中idref和ref两个属性的使用方法,下面我们来学习对一些集合类型的注入配置主要是List,Set,Map和Properties文件。
下面我们还是通过一个例子来演示集合注入。
首先新建一个类
CollectionInjection.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionInjection {
private List list;
private Set set;
private Map map;
private Properties pro;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
}
我们这里保留上一章的两个类TargetBean.java,ClientBean.java
配置文件bean.xml如下:
<bean id="TargetBean" class="cn.com.spring.injection.bean.TargetBean">
<property name="name" value="targetBean" />
</bean>
<bean id="ClientBean" class="cn.com.spring.injection.bean.ClientBean">
<property name="tb">
<ref local="TargetBean" />
</property>
<property name="flag" value="clientBean" />
</bean>
<bean id="CollectionInjec" class="cn.com.spring.injection.bean.CollectionInjection">
<property name="list">
<list>
<value>List Injection String Test</value>
<ref bean="ClientBean" />
</list>
</property>
<property name="set">
<set>
<value>Set Injection String Test</value>
<ref bean="ClientBean" />
</set>
</property>
<property name="map">
<map>
<entry>
<key><value>test</value></key>
<value>Map Injection Test</value>
</entry>
<entry>
<key><value>bean</value></key>
<ref bean="ClientBean" />
</entry>
</map>
</property>
<property name="pro">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
</bean>
测试程序,我们还是使用Junit来进行测试,代码如下
public class TestUtils extends TestCase {
private ApplicationContext actx = new FileSystemXmlApplicationContext("bean.xml");
@Test
public void test1(){
ClientBean cbean = null;
CollectionInjection clij = (CollectionInjection)actx.getBean("CollectionInjec");
// List集合里的数据是按顺序存放的所以可以按照位置取
List list = clij.getList();
System.out.println((String)list.get(0));
cbean = (ClientBean)list.get(1);
System.out.println(cbean.getFlag());
// Map集合是键值对
Map map = clij.getMap();
System.out.println((String)map.get("test"));
cbean = (ClientBean)map.get("bean");
System.out.println(cbean.getFlag());
// Set集合里的内容是无须因此不能使用按位置顺序取证,这里采用将其转化成Iterator进行迭代
Set set = clij.getSet();
Iterator it = set.iterator();
Object obj = null;
while(it.hasNext()){
obj = it.next();
if(obj instanceof String){
System.out.println(obj);
}
if(obj instanceof ClientBean){
cbean = (ClientBean)obj;
System.out.println(cbean.getFlag());
}
}
// Properties取值
Properties pro = clij.getPro();
System.out.println(pro.getProperty("administrator"));
System.out.println(pro.getProperty("support"));
System.out.println(pro.getProperty("development"));
}
}
测试结果
List Injection String Test
clientBean
Map Injection Test
clientBean
Set Injection String Test
clientBean
administrator@example.org
support@example.org
development@example.org
和我们注入的参数一致
这里面大家可能会有一个疑问就是对于集合Java提供了泛型支持,那怎么在Spring配置里面体现呢,有没有什么属性配合泛型支持呢,事实上Spring只管进行注入,Spring把这个工作给了开发者,也就是说你如果对集合里的内容做了类型限制,那么你在配置的时候就应该遵守你的限制进行配置,否则Spring会因类型转换异常而无法注入然后报错。
这里补充一点关于空值的注入也就是如果你想对给一个属性赋值null该怎么配置,Spring提供了两种形式的配置
<bean class="ExampleBean">
<property name="email"><value/></property>
</bean>
和
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
经过测试发现第一种写法如果对于String类型的属性,它赋值为空而并不是Null,对于其他类型则会报错。所以如果想对属性赋null值最好是采用第二种写法。