<list> / <array>:有序、可重複 → 注入 List 或數組<set>:無序、不重複 → 注入 Set<map>:鍵值對 → 注入 Map<props>:String 鍵值對 → 注入 Properties<bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean" scope="singleton">
<!-- Set 注入 -->
<property name="sets">
<set>
<value>岑紅軍</value>
<value>軍哥</value>
<value>哈哈</value>
<value>呵呵</value>
<value>嘿嘿</value>
<value>洗洗</value>
</set>
</property>
<!-- List/Array 注入(引用其他 Bean) -->
<property name="users">
<array>
<ref bean="u1"/>
<ref bean="u2"/>
<ref bean="u3"/>
<ref bean="u4"/>
</array>
</property>
<!-- Map 注入 -->
<property name="map">
<map>
<entry key="1" value-ref="u1"/>
<entry key="2"><ref bean="u2"/></entry>
<entry key="3" value-ref="u3"/>
</map>
</property>
<!-- Properties 注入 -->
<property name="props">
<props>
<prop key="1">jdbc:oracle</prop>
<prop key="2">jdbc:mysql</prop>
<prop key="3">jdbc:access</prop>
</props>
</property>
</bean>
<!-- 被引用的 User Bean -->
<bean id="u1" class="www.csdn.spring.collection.set.User"><property name="name" value="deep"/><property name="age" value="21"/></bean>
<bean id="u2" class="www.csdn.spring.collection.set.User"><property name="name" value="deepsoul"/><property name="age" value="22"/></bean>
<bean id="u3" class="www.csdn.spring.collection.set.User"><property name="name" value="chrp"/><property name="age" value="23"/></bean>
<bean id="u4" class="www.csdn.spring.collection.set.User"><property name="name" value="redarmy"/><property name="age" value="28"/></bean>
public class User {
private String name; private Integer age;
// getter/setter/toString
}
public class CollectionBean {
private Set sets;
private List users;
private Map map;
private Properties props;
// getter/setter
}
public class TestBean {
@Test
public void tests() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-collection.xml");
CollectionBean bean = context.getBean("collectionBean", CollectionBean.class);
// Set 遍歷
System.out.println("---set---");
for (String s : bean.getSets()) System.out.println(s);
// List 遍歷
System.out.println("---list---");
for (User u : bean.getUsers()) System.out.println(u.getName() + " " + u.getAge());
// Map 遍歷方式 1:keySet
System.out.println("---map1---");
for (Integer key : bean.getMap().keySet()) {
User u = bean.getMap().get(key);
System.out.println(key + " " + u.getName() + " " + u.getAge());
}
// Map 遍歷方式 2:entrySet(推薦)
System.out.println("---map2---");
for (Map.Entry entry : bean.getMap().entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue().getName() + " " + entry.getValue().getAge());
}
// Properties 遍歷
System.out.println("---props---");
for (String key : bean.getProps().stringPropertyNames()) {
System.out.println(key + " " + bean.getProps().getProperty(key));
}
}
}
---set---
岑紅軍
軍哥
哈哈
呵呵
嘿嘿
洗洗
---list---
deep 21
deepsoul 22
chrp 23
redarmy 28
---map1---
1 deep 21
2 deepsoul 22
3 chrp 23
---map2---
1 deep 21
2 deepsoul 22
3 chrp 23
---props---
3 jdbc:access
2 jdbc:mysql
1 jdbc:oracle
<ref bean="..."/> 引用其他 Bean,<value> 注入字面值<entry> 支持 key/value/value-ref/key-ref 多種組合<props> 專為 Properties 設計,鍵值皆為 StringentrySet(),單次查找避免雜湊計算<property> 同名會合並(需啟用合並屬性)掌握四大集合注入語法,配置數據源、緩存配置、模板引擎參數等場景得心應手。