前言
这几天放假,昨天把网站主题稍微改了下,所以没继续更新,今天趁热打铁,继续spring新手攻略系列。有时候真的是需要长期坚持,虽然文章不长,也没什么复杂的东西,但是坚持完成却似乎很难。因为每次都会把某系列文章更新到一半就没了,所以这次决定下狠心一定要写完了,所以,今天我们继续,今天主要讲下spring注入中对于集合类(Collection)的注入。如果你看了本系列的前几篇文章,对于今天的内容,其实是非常熟悉的,其实说到底还是注入的概念,只是注入的东西不一样了,而且你会发现使用spring其实只要遵循它规定的那种模式,其实使用起来非常方便,当然这里只是指对于初级玩家使用。为什么这么说,因为spring用起来非常简单,但是本身框架的设计确有很多可以学习鉴戒之处,对于这部分,希望今后能有机会写写。好吧,那么今天就正式进入主题:
几种注入的集合配置元素
- 《list》使用该配置元素,可以注入一个list,这个list可以包含重复元素值
- 《set》使用该配置元素,可以注入一个set集合,不能包含重复元素
- 《map》使用该配置元素,就是注入一个map类型,包含任意类型的键值对元素。
- 《props》使用该配置元素,注入一个集合,里面是string类型的键值对元素。
上述几种配置元素,你应该你能猜到,它们就是bean的xml配置文件中的元素,你可以使用
或者
来放任何 java.util.Collection 的实现类或者数组(array)。另外对于放入集合的元素可以是基本类型或者是另一个bean的引用。下面来看一个综合示例:
package sirk_spring_tuto.demo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionsDemo { private List<String> nameList; private Set<String> nameSet; private Map<String, String> nameMap; private Properties nameProp; public List<String> getNameList() { return nameList; } public void setNameList(List<String> nameList) { this.nameList = nameList; } public Set<String> getNameSet() { return nameSet; } public void setNameSet(Set<String> nameSet) { this.nameSet = nameSet; } public Map<String, String> getNameMap() { return nameMap; } public void setNameMap(Map<String, String> nameMap) { this.nameMap = nameMap; } public Properties getNameProp() { return nameProp; } public void setNameProp(Properties nameProp) { this.nameProp = nameProp; } } package sirk_spring_tuto.demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CollectionsMain { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("CollectionBeans.xml"); CollectionsDemo collectionsDemo = (CollectionsDemo) ctx.getBean("collectionsDemo"); System.out.println(collectionsDemo.getNameList()); System.out.println(collectionsDemo.getNameSet()); System.out.println(collectionsDemo.getNameMap()); System.out.println(collectionsDemo.getNameProp()); } } <?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-3.1.xsd"> <bean id="collectionsDemo" class="sirk_spring_tuto.demo.CollectionsDemo"> <property name="nameList"> <list> <value>hello</value> <value>sirk</value> <value>hello</value> </list> </property> <property name="nameSet"> <set> <value>hello</value> <value>sirk</value> <value>hello</value> </set> </property> <property name="nameMap"> <map> <entry key="hello" value="sirk"/> <entry key="good" value="day"/> </map> </property> <property name="nameProp"> <props> <prop key="one">hello</prop> <prop key="two">world</prop> </props> </property> </bean> </beans>
最后执行打印如下结果:
[hello, sirk, hello] [hello, sirk] {hello=sirk, good=day} {two=world, one=hello}
注入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-3.1.xsd"> <bean id="collectionsDemo" class="sirk_spring_tuto.demo.CollectionsDemo"> <property name="nameList"> <list> <ref bean="XXX"/> <value>hello</value> <value>sirk</value> <value>hello</value> </list> </property> <property name="nameSet"> <set> <ref bean="XXX"/> <value>hello</value> <value>sirk</value> <value>hello</value> </set> </property> <property name="nameMap"> <map> <entry key="hello" value="sirk"/> <entry key="good" value="day"/> <entry key="YYY" value-ref="XXX"></entry> </map> </property> <property name="nameProp"> <props> <prop key="one">hello</prop> <prop key="two">world</prop> </props> </property> </bean> </beans>
这配置其实混合使用了值类型和引用类型,请注意那些带有ref的标签元素。就是对于引用类型的用法。
注入null和空字符串
最后再来看看如何注入null和空字符串:
如果想注入null,请看下面配置
<bean id="..." class="exampleBean"> <property name="telephone"><null/></property> </bean>
这好比调用了exampleBean.setTelephone(null)
如果想注入空字符串,看下面配置:
<bean id="..." class="exampleBean"> <property name="telephone" value=""/> </bean>
该配置好比调用了exmapleBean.setTelephone(“”)
小结
本文介绍了spring对于集合的注入,下篇更精彩,敬请期待~
##文档信息
- 版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0
- 原文网址:http://www.cocosk.com/articles/2014⁄4/7/spring-injecting-collection.html
- 作者:卧雪Sirk