業務介面定義
public interface IPersonService {
void save();
Set<String> getSets();
List<String> getLists();
Properties getProperties();
Map<String, String> getMaps();
}
實作類別
public class PersonServiceBean implements IPersonService {
private IPersonDao personDao;
private String name; // 建構子注入
// 集合屬性
private Set<String> sets = new HashSet<>();
private List<String> lists = new ArrayList<>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<>();
// 建構子注入:Dao + name
public PersonServiceBean(IPersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
}
// Getter/Setter 略...
public void save() {
System.out.println(name); // 輸出建構子注入的 name
personDao.add();
}
}
Spring XML 配置
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 目標 Bean -->
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<!-- 建構子注入:index 0=Dao, index 1=String -->
<constructor-arg index="0" ref="personDaoBean" type="cn.itcast.dao.IPersonDao"/>
<constructor-arg index="1" type="java.lang.String" value="傳智博客"/>
<!-- 集合屬性注入 -->
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="properties">
<props>
<prop key="properties1">property1</prop>
<prop key="properties2">property2</prop>
<prop key="properties3">property3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key1" value="keyFirst"/>
<entry key="key2" value="keySecond"/>
<entry key="key3" value="keyThird"/>
</map>
</property>
</bean>
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"/>
</beans>
測試驗證
@Test
public void testInjection() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IPersonService service = (IPersonService) ctx.getBean("personService");
// 遍歷輸出
System.out.println("=== Set ===");
service.getSets().forEach(System.out::println);
System.out.println("=== List ===");
service.getLists().forEach(System.out::println);
System.out.println("=== Properties ===");
service.getProperties().keySet().forEach(System.out::println);
System.out.println("=== Map ===");
service.getMaps().keySet().forEach(System.out::println);
service.save(); // 輸出: 傳智博客 + Dao 邏輯
}
執行結果
=== Set ===
set1
set2
set3
=== List ===
list1
list2
list3
=== Properties ===
properties3
properties2
properties1
=== Map ===
key1
key2
key3
傳智博客
這是 personDaoBean 的 Add() 方法
關鍵點總結
| 集合型別 | XML 元素 | 特性 |
| List | <list> | 有序、可重複 |
| Set | <set> | 無序、唯一 |
| Map | <map> + <entry> | 鍵值對、key 唯一 |
| Properties | <props> + <prop> | String 鍵值、可直接 load/store |
- 建構子注入適合**強制依賴**(不可變物件、線程安全)
- Setter 注入適合**可選依賴**
ref 參考其他 Bean,value 直接字面值
- 複雜集合可巢狀:
<list><ref bean="..."/></list>