`
tanglei198577
  • 浏览: 57903 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

Wiring collections , from java in action

    博客分类:
  • SSH
阅读更多

The method implements:

public class SingleBand implements Performer{

	public SingleBand(){}
	
	private Collection<Instrument> instruments;
	
	public void setInstrument(Collection<Instrument> instruments){
		this.instruments = instruments;
	}
	
	public void perform() {
		// TODO Auto-generated method stub
		for(Instrument instrument: instruments){
			instrument.play();
		}
	}

}

 the bean settings:

   

    <bean id="band" class="com.performence.SingleBand">
       <property name="instrument">

         <list>
           <ref bean='piano'/>
           <ref bean='voilin'/>
           <ref bean='piano'/>
           <ref bean='voilin'/>
         </list>
       </property>
    </bean>
   <bean id="piano" class="com.performence.Piano"/>
   <bean id="voilin" class="com.performence.Violin"/>
   
</beans>

 

test method :

       

   public static void main(String args[]){
	   BeanFactory factory = new XmlBeanFactory(new FileSystemResource("bean.xml"));
	   Performer per = (Performer)factory.getBean("band");
	   per.perform();
   }
}

 

output:

   piano playing.......
   violin playing.......
   piano playing.......
   violin playing.......

 

if change the <list> to <set> in the bean.xml

output :

   piano playing.......
   violin playing.......

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics