2.2.3使用Spring的命名空间p装配属性。。。
2.2.4装配集合
装配List、Set、Array。。。
装配Map。。。
装配Properties
配置文件spring-idol.xml代码:
<!-- 2.2.4装配集合。 -->
<bean id = "hank"
class = "com.springinaction.springidol.OneManBand">
<property name = "instruments">
<props>
<prop key = "GUITAR" >STRUM STRUM STRUM</prop>
<prop key = "CYMBAL" >CRASH CRASH CRASH</prop>
<prop key = "HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>
OneManBand.java代码:
package com.springinaction.springidol;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
public class OneManBand implements Performer{
private Properties instruments;
public OneManBand(){};
public void perform(){
for(Entry<Object, Object> entry:instruments.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue().toString());
}
}
public void setInstruments(Properties instruments){//开始setInstruments我把最后面s忘写了,结果不能成功创建bean
this.instruments = instruments; //以Map类型注入instrument
}
}
运行结果: