您的位置:首页 > 编程语言 > Java开发

Spring之注入集合值

2016-02-27 20:04 483 查看
如果需要调用形参类型为集合的setter方法,或调用形参类型为集合的构造器,则可使用集合元素<list…/>、<set…/>、<map…/>和<props…/>分别来设置类型为List、Set、Map和Properties的集合参数值。下面笔者通过代码来展示给大家看,相信,聪明的你们一看代码就知道注入集合值是怎么回事及怎么用了。

1) 定义Chinese.java的类。

public class Chinese implements Person
{
// 下面是系列集合类型的成员变量
private List<String> schools;
private Map scores;
private Map<String , Axe> phaseAxes;
private Properties health;
private Set axes;
private String[] books;

public Chinese()
{
System.out.println("Spring实例化主调bean:Chinese实例...");
}

// schools的setter方法
public void setSchools(List schools)
{
this.schools = schools;
}
// scores的setter方法
public void setScores(Map scores)
{
this.scores = scores;
}
// phaseAxes的setter方法
public void setPhaseAxes(Map<String , Axe> phaseAxes)
{
this.phaseAxes = phaseAxes;
}
// health的setter方法
public void setHealth(Properties health)
{
this.health = health;
}
// axes的setter方法
public void setAxes(Set axes)
{
this.axes = axes;
}
// books的setter方法
public void setBooks(String[] books)
{
this.books = books;
}

// 访问上面全部的集合类型的成员变量
public void test()
{
System.out.println(schools);
System.out.println(scores);
System.out.println(phaseAxes);
System.out.println(health);
System.out.println(axes);
System.out.println(java.util.Arrays.toString(books));
}
}


2) beans.xml的文件配置。

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定义2个普通Axe Bean -->
<bean id="stoneAxe" class="com.owen.app.service.impl.StoneAxe"/>
<bean id="steelAxe" class=" com.owen.app.service.impl.SteelAxe"/>
<!-- 定义chinese Bean -->
<bean id="chinese" class=" com.owen.app.service.impl.Chinese">
<property name="schools">
<!-- 为调用setSchools()方法配置List集合作为参数值 -->
<list>
<!-- 每个value、ref、bean...都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为调用setScores()方法配置Map集合作为参数值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="数学" value="87"/>
<entry key="英语" value="89"/>
<entry key="语文" value="82"/>
</map>
</property>
<property name="phaseAxes">
<!-- 为调用setPhaseAxes()方法配置Map集合作为参数值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="原始社会" value-ref="stoneAxe"/>
<entry key="农业社会" value-ref="steelAxe"/>
</map>
</property>
<property name="health">
<!-- 为调用setHealth()方法配置Properties集合作为参数值 -->
<props>
<!-- 每个prop元素配置一个属性项,其中key指定属性名 -->
<prop key="血压">正常</prop>
<prop key="身高">175</prop>
</props>
<!--
<value>
pressure=normal
height=175
</value> -->
</property>
<property name="axes">
<!-- 为调用setAxes()方法配置Set集合作为参数值 -->
<set>
<!-- 每个value、ref、bean..都配置一个Set元素 -->
<value>普通的字符串</value>
<bean class=" com.owen.app.service.impl.SteelAxe"/>
<ref bean="stoneAxe"/>
<!-- 为Set集合配置一个List集合作为元素 -->
<list>
<value>20</value>
<!-- 再次为List集合配置一个Set集合作为元素 -->
<set>
<value type="int">30</value>
</set>
</list>
</set>
</property>
<property name="books">
<!-- 为调用setBooks()方法配置数组作为参数值 -->
<list>
<!-- 每个value、ref、bean...都配置一个数组元素 -->
<value>疯狂Java讲义</value>
<value>疯狂Android讲义</value>
<value>轻量级Java EE企业应用实战</value>
</list>
</property>
</bean>
</beans>

当使用<list…/>、<set…/>、<map…/>等元素配置集合类型的参数值时,还需要配置集合元素。由于集合元素又可以是基本类型值、引用容器中的其他Bean、嵌套Bean或集合属性等,所以<list…/>、<key…/>和<set…/>元素又可接受如下子元素:

1) value:指定集合元素是容器基本类型或字符串类型。

2) ref:指定集合元素是容器中另一个Bean实例。

3) bean:指定集合元素是一个Bean实例。

4) list、set、map及props:指定集合元素又是集合。

<props…/>元素用于配置Properties类型的参数值,Properties类型是一种特殊的类型,其key和value都只能是字符串,故Spring配置Properties类型的参数值比较简单:每个key-value对只要分别给出key和value就足够了——而且key和value都是字符串类型,所以使用如下格式的<prop…/>元素就够了。

1) <prop key=”血压”>正常</prop>,其中<prop…/>元素的key属性指定key值,<prop…/>元素的内容指定value的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: