Spring配置多个属性值,或者spring装配list、array、set、map等方式时可以使用如下方式,比如,这里有个人会演奏很多的乐器,那么需要有乐器Instrument这个接口,然后定义具体的乐器去实现这个乐器接口,还有一个表演者Performer这个接口,因为不只是只有这一个表演者可能。下面具体讲解
(1)建立表演者接口Performer
package com.springaction.P30;
public interface Performer {
void perform() ;
}
(2)建立Instrument这个接口package com.springaction.P40;
public interface Instrument {
public void play();
}
(3)建立OneManHand(一个多才多艺的表演者)这个类,该类要实现Performer,并且内部维护了乐器Instrument这个对象,一般内部定义个全局属性,都会有相应的set方法,以便xml文件中进行IOC
package com.springaction.P46;
import java.util.Collection;
import java.util.Map;
import com.springaction.P30.Performer;
import com.springaction.P40.Instrument;
public class OneManBand implements Performer{
//对list进行测试
/* private Collection<Instrument> instruments;
public void setInstruments(Collection<Instrument> instruments) {
this.instruments = instruments;
}
@Override
public void perform() {
for(Instrument instrument:instruments) {
instrument.play();
}
}*/
private Map<String,Instrument> instruments;
public void setInstruments(Map<String,Instrument> instruments) {
this.instruments = instruments;
}
@Override
public void perform() {
for(String key:instruments.keySet()) {
System.out.println(key+" :");
Instrument instrument = instruments.get(key);
instrument.play();
}
}
}
(4)编辑Spring的xml文件,将上面的类OneManHand注入到bean中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sonnet29" class="com.springaction.P34.Sonnet29">
</bean>
<bean id="duck" class="com.springaction.P30.PoeticJuggler">
<constructor-arg value="15"></constructor-arg>
<constructor-arg ref="sonnet29"></constructor-arg>
<!-- <constructor-arg value="小名"></constructor-arg>-->
</bean>
<bean id="kenny" class="com.springaction.P40.Instrumentalist">
<property name="song" value="我爱你中国"></property>
<property name="instrument" ref="saxophone"></property>
</bean>
<bean id="saxophone" class="com.springaction.P40.Saxophone">
</bean>
<!-- List测试代码部分
<bean id="hank" class="com.springaction.P46.OneManBand">
<property name="instruments">
<list>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="hormonica"/>
</list>
</property>
</bean>
-->
<bean id="hank" class="com.springaction.P46.OneManBand">
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="saxophone"></entry>
<entry key="CYMBAL" value-ref="saxophone"></entry>
<entry key="HARMONICA" value-ref="saxophone"></entry>
</map>
</property>
</bean>
</beans>
(5)测试代码部分
package com.springaction.P30;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PerformerTest {
@Test
public void testPerform() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Performer performer = (Performer)ctx.getBean("duck");
Performer performer2 = (Performer)ctx.getBean("hank");
performer.perform();
performer2.perform();
}
}
(6)结果展示:
GUITAR :
Ding Ding Ding
CYMBAL :
Ding Ding Ding
HARMONICA :
Ding Ding Ding