接上一篇
property的用法
property标签内部的属性也是ref和value,跟 constructor-arg用法无异,所以这里结合 集合 装配来说
property标签跟 constructor-arg 区别在于 后者是找构造方法,前者是找类的字段。先上代码
先写一个Instruments(乐器)接口
package spring.ioc03;
public interface Instruments {
public void play();
}
再写三个乐器,实现Instruments接口
package spring.ioc03;
public class Cello implements Instruments {
@Override
public void play() {
System.out.print("大提琴 ");
}
}
package spring.ioc03;
public class Piano implements Instruments {
@Override
public void play() {
System.out.print("钢琴 ");
}
}
package spring.ioc03;
public class Violin implements Instruments {
@Override
public void play() {
System.out.print("小提琴 ");
}
}
最后写一个Pan(演奏者),他分别用list,map,set三种集合类型来保存自己会的乐器。还有一个performance方法,展示自己会的乐器。
package spring.ioc03;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Pan {
private List<Instruments> instrumentsList;
private Map<String, Instruments> instrumentsMap;
private Set<Instruments> instrumentsSet;
public void performance() {
System.out.print("list我会 ");
for (Instruments instrument : instrumentsList)
instrument.play();
System.out.println();
System.out.print("map我会 ");
for (Instruments instrument : instrumentsMap.values())
instrument.play();
System.out.println();
System.out.print("set我会 ");
for (Instruments instrument : instrumentsSet)
instrument.play();
}
public List<Instruments> getInstrumentsList() {
return instrumentsList;
}
public void setInstrumentsList(List<Instruments> instrumentsList) {
this.instrumentsList = instrumentsList;
}
public Map<String, Instruments> getInstrumentsMap() {
return instrumentsMap;
}
public void setInstrumentsMap(Map<String, Instruments> instrumentsMap) {
this.instrumentsMap = instrumentsMap;
}
public Set<Instruments> getInstrumentsSet() {
return instrumentsSet;
}
public void setInstrumentsSet(Set<Instruments> instrumentsSet) {
this.instrumentsSet = instrumentsSet;
}
}
最后看一下spring的配置文件, property 标签的name属性对应类的 某个字段,比如instrumentsList,instrumentsSet,instrumentsMap,它会在spring装配bean的时候自动为这些字段赋值(通过字段的set方法),然后集合的装配方法见下面的配置。以达到装配pan的目的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<bean id="pan" class="spring.ioc03.Pan">
<!-- list -->
<property name="instrumentsList">
<list>
<bean class="spring.ioc03.Cello"></bean>
<bean class="spring.ioc03.Piano"></bean>
<bean class="spring.ioc03.Violin"></bean>
</list>
</property>
<!-- Set -->
<property name="instrumentsSet">
<set>
<bean class="spring.ioc03.Cello"></bean>
<bean class="spring.ioc03.Piano"></bean>
<bean class="spring.ioc03.Violin"></bean>
</set>
</property>
<!-- Map -->
<property name="instrumentsMap">
<map>
<entry key="1" >
<bean class="spring.ioc03.Cello"></bean>
</entry>
<entry key="2" >
<bean class="spring.ioc03.Piano"></bean>
</entry>
<entry key="3" >
<bean class="spring.ioc03.Violin"></bean>
</entry>
</map>
</property>
</bean>
</beans>
最后的单元测试
/**
* ioc03
* 装配List,map,set
*/
@Test
public void test4() {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc03.xml");
spring.ioc03.Pan pan = (spring.ioc03.Pan) context.getBean("pan");
pan.performance();
}
输出结果为
list我会 大提琴 钢琴 小提琴
map我会 大提琴 钢琴 小提琴
set我会 大提琴 钢琴 小提琴