Spring学习笔记_装配bean_02

Spring的属性的注入包括单个properties的注入,list,set,map,properties集中类型。下面是代码示例。

1、list的属性注入

applicationContext的配置:

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		 http://www.springframework.org/schema/beans/spring-beans.xsd 
    		http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">  
 	
 	<bean id="saxophone" class="com.csii.spring.Saxophone"></bean>
 	
 	<bean id="guitar" class="com.csii.spring.Guitar"></bean>
 	
 	<bean id="piano" class="com.csii.spring.Piano"></bean>
 	
 	<!-- 注入list集合 -->
 	<bean id="Jack" class="com.csii.spring.OneManBand">
 	    <property name="instruments">
 	        <list>
 	            <ref bean="saxophone"/>
 	            <ref bean="guitar"/>
 	             <ref bean="piano"/>
 	        </list>
 	    </property>
 	</bean>
 	
</beans>  
然后新建一下几个类:乐器类Instrument.java,钢琴类Piano.java,萨克斯类:Saxophone,吉它类Guitar.java,一人乐队类OneManBand.java

public interface Instrument {
	
	public void paly();
}
public class Guitar implements Instrument{

	Logger logger=LoggerFactory.getLogger(Saxophone.class);

	public void paly() {
		
		logger.info("******吉它表演。。。******");
		
	}

}
public class Saxophone implements Instrument{
	
	Logger logger=LoggerFactory.getLogger(Saxophone.class);

	public void paly() {
		
		logger.info("******萨克斯表演。。。******");
		
	}

}
public class Piano implements Instrument{

	Logger logger=LoggerFactory.getLogger(Saxophone.class);

	public void paly() {
		
		logger.info("******钢琴表演。。。******");
		
	}

}
public class OneManBand implements Performer{
	
	private List<Instrument> instruments;

	public void perform() throws Exception {
		for (Instrument instrument : instruments) {
			instrument.paly();
		}
		
	}
	public void setInstruments(List<Instrument> instruments) {
		this.instruments = instruments;
	}

}
然后写一个测试类就可以测试类:

public class Test {
	
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx=
	      		   new ClassPathXmlApplicationContext("applicationContext.xml");
		OneManBand oneManBand=(OneManBand)ctx.getBean("Jack");
		oneManBand.perform();
	    
	}

}
输出结果:

  INFO  2015-05-31 21:48:53,235 [message] ******萨克斯表演。。。******
    INFO  2015-05-31 21:48:53,235 [message] ******吉它表演。。。******
    INFO  2015-05-31 21:48:53,235 [message] ******钢琴表演。。。******

2、Set属性注入跟list集合注入差不多,list允许元素有重复的值,set不能有重复值。xml中的配置也类似。

<bean id="Jack" class="com.csii.spring.OneManBand">
 	    <property name="instruments">
 	        <set>
 	            <ref bean="saxophone"/>
 	            <ref bean="guitar"/>
 	             <ref bean="piano"/>
 	        </set>
 	    </property>
 	</bean>

3、Map属性注入

applicationContext.xml中的配置

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		 http://www.springframework.org/schema/beans/spring-beans.xsd 
    		http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">  
   
 	<bean id="stage" class="com.csii.spring.Stage" factory-method="getInstance"></bean>
 	
 	<bean id="saxophone" class="com.csii.spring.Saxophone"></bean>
 	
 	<bean id="guitar" class="com.csii.spring.Guitar"></bean>
 	
 	<bean id="piano" class="com.csii.spring.Piano"></bean>
 	
 	<!-- Map注入 -->
 	<bean id="Lucy" class="com.csii.spring.OnemanBandMap">
 	    <property name="instruments">
 	        <map>
 	            <entry key="SAXOPHONE" value-ref="saxophone"></entry>
 	             <entry key="GUITAR" value-ref="guitar"></entry>
 	              <entry key="PIANO" value-ref="piano"></entry>
 	        </map>
 	    </property>
 	</bean>
 	
</beans>  
然后新建一个一人乐队类:OnemanBandMap.java(其它几个类同上面)

public class OnemanBandMap implements Performer{
	
	private Map<String,Instrument> instruments;

	public void perform() throws Exception {
		
		for (String key : instruments.keySet()) {
			Instrument instrument=instruments.get(key);
			instrument.paly();
		}
	}

	public void setInstruments(Map<String, Instrument> instruments) {
		this.instruments = instruments;
	}

}
测试类:

public class Test {
	
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx=
	      		   new ClassPathXmlApplicationContext("applicationContext.xml");
		 OnemanBandMap lucy=(OnemanBandMap)ctx.getBean("Lucy");
		    lucy.perform();
	    
	}

}
输出结果:

   INFO  2015-05-31 21:55:07,599 [message] ******萨克斯表演。。。******
    INFO  2015-05-31 21:55:07,599 [message] ******吉它表演。。。******
    INFO  2015-05-31 21:55:07,599 [message] ******钢琴表演。。。******













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值