// List
<bean id="userService" class="com.spring.service.UserService">
<property name="list">
<list>
<value>aaaa</value>
<value>bbbb</value>
</list>
</property>
</bean>
//set
<bean id="userService" class="com.spring.service.UserService">
<property name="set">
<set>
<value>cccc</value>
<value>dddd</value>
</set>
</property>
</bean>
//map
<bean id="userService" class="com.spring.service.UserService">
<property name="map">
<map>
<entry key="1111" value="aaaa"/>
<entry key="2222" value="bbbb"/>
</map>
</property>
</bean>
//properties
<bean id="userService" class="com.spring.service.UserService">
<property name="pro">
<props>
<prop key="1111">aaaa</prop>
<prop key="2222">cccc</prop>
</props>
</property>
</bean>
具体实例
public class Chinese implements Person{
//下面是一系列的集合属性
private List<String> schools;
private Map scores;
private Map<String, Axe> phaseAxes;
private Properties health;
private Set axe;
private String[] books;
public List<String> getSchools() {
return schools;
}
public void setSchools(List<String> schools) {
this.schools = schools;
}
public Map getScores() {
return scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public Map<String, String> getPhaseAxes() {
return phaseAxes;
}
public void setPhaseAxes(Map<String, String> phaseAxes) {
this.phaseAxes = phaseAxes;
}
public Properties getHealth() {
return health;
}
public void setHealth(Properties health) {
this.health = health;
}
public Set getAxe() {
return axe;
}
public void setAxe(Set axe) {
this.axe = axe;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public void useAxe() {
}
}
上面的java代码中有数组、list、set、,map、Properties。下面是针对上面的配置文件。
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <!-- 定义一个普通的Axe Bean --> <bean id="steelAxe" class="com.spring.service.impl.SteelAxe" /> <bean id="stoneAxe" class="com.spring.service.impl.StoneAxe" /> <!--定义Chinese Bean --> <bean id="chinese" class="com.spring.service.impl.Chinese"> <property name="schools"> <list> <value>小学</value> <value>中学</value> <value>大学</value> </list> </property> <property name="scores"> <map> <entry key="语文" value="88" /> <entry key="数学" value="87" /> <entry key="外语" value="88" /> </map> </property> <property name="phaseAxes"> <map> <entry key="原始社会" value-ref="stoneAxe" /> <entry key="农业社会" value-ref="steelAxe" /> </map> </property> <property name="health"> <props> <prop key="血压">正常</prop> <prop key="身高">175</prop> </props> </property> <property name="axe"> <set> <value>普通字符串</value> <bean class="com.spring.service.impl.SteelAxe"></bean> <ref local="stoneAxe"/> </set> </property> <property name="books"> <list> <value>java 编程思想</value> <value>思考致富</value> <value>将才</value> </list> </property> </bean> </beans>
从上面的配置文件中可以看出,Spring对list属性和数组属性的处理是一样的。
当我们使用<list.../>、<set.../>、<map.../>等元素配置集合属性时,我们还需要手动配置集合元素。由于集合元素又可以是基本类型值、引用容器中的其他Bean、嵌套Bean和集合属性等。所以这些元素又可以接受如下子元素:
value:指定集合元素是基本数据类型或者字符类型值。
ref:指定集合元素是容器中另一个Bean实例。
bean:指定集合元素是一个嵌套Bean。
list、set、map、props:指定集合元素值又是集合。