Java类中属性是list、map、set、properties时,可用元素<list><map><set><props>配置。
示例如下:
java类InCollections代码:
public class InCollections {
private Set<String> sets;
private List<String> lists;
private Map<String, String> maps;
private Properties properties;
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String toString() {
return sets.toString() + lists.toString() + maps.toString() + properties.toString();
}
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
InCollections inCollections = (InCollections) context.getBean("collections");
System.out.println(inCollections);
}
}
配置文件beans.xml如下:
<bean id="collections" class="com.decipering.InjectCollection.InCollections">
<property name="sets">
<!-- 使用<set>元素设置sets属性,一个set元素设置一个集合,value元素表示每一个集合元素,以下同理 -->
<set>
<value>1</value>
<value>2</value>
</set>
</property>
<property name="lists">
<list>
<value>100</value>
<value>200</value>
<value>300</value>
</list>
</property>
<property name="maps">
<map>
<!-- 一个entry元素记录一个键值对,其key值只能是String类型 -->
<entry key="1" value="10"></entry>
<entry key="2" value="20"></entry>
<entry key="3" value="30"></entry>
</map>
<!-- 或者
<map>
<entry key="1" >
<value>"10"</value>
</entry>
<entry key="2" >
<value>"20"</value>
</entry>
<entry key="3" >
<value>"30"</value>
</entry>
</map> -->
</property>
<property name="properties">
<props>
<!-- properties集合的键和值都是字符串类型 -->
<prop key="5">50</prop>
<prop key="6">60</prop>
<prop key="7">70</prop>
</props>
</property>
</bean>