Spring 复杂对象的注入

                                                                            Spring 复杂对象的注入

    复杂对象主要分为有两类:Collection类(包括数组和list)和Map类。

    List<String>的注入形式:(数组形式其实是差不多的)

public class OneManBand implements Performer {
	public OneManBand() {
	}

	public void perform() throws PerformanceException {
		for (Instrument instrument : instruments) {
			instrument.play();
		}
	}

	private Collection<Instrument> instruments;

	private Collection<String> names;

	public Collection<String> getNames() {
		return names;
	}

	public void setNames(Collection<String> names) {
		this.names = names;
	}

	public void setInstruments(Collection<Instrument> instruments) {
		this.instruments = instruments; // <co
										// id="co_injectInstrumentCollection"/>
	}
	public static void main(String args[]){
		ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml");
		OneManBand one=(OneManBand)ac.getBean("oneManBand");
		System.out.println(one.getNames());
	}
}
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <!-- 说明是getter/setter注入-->	
	<bean id="oneManBand" class="com.springinaction.springidol.OneManBand">
	<property name="names">
	
	<list>
 <!-- 内部bean	<bean></bean>  -->
  <!-- 引用 <ref bean="xx"></ref>  -->
    <!-- 常规类型主要就是 就用 value-->	
	<value>1</value>
	<value>nihao</value>
	</list>
	</property>
	</bean>

</beans>

    map<String,Integer> 注入:

public class OneManBand implements Performer {
	
	private Collection<Instrument> instruments;
	
	private Map<String,Integer> prices;
	public Map<String, Integer> getPrices() {
		return prices;
	}

	public void setPrices(Map<String, Integer> prices) {
		this.prices = prices;
	}

	
	public static void main(String args[]){
		ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml");
		OneManBand one=(OneManBand)ac.getBean("oneManBand");
		System.out.println(one.getPrices());
	}
}

    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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="oneManBand" class="com.springinaction.springidol.OneManBand">
		<property name="prices">
			<map>
			<entry key="1" value="2"></entry>
			<entry key="2" value="3"></entry>
			</map>
		</property>
	</bean>

</beans>


转载于:https://my.oschina.net/QAAQ/blog/655757

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值