注入集合值

如果Bean的属性是个集合,则可以使用集合元素,<list.../>、<set.../>、<map.../>和<props.../>元素分别用来设置类型为List、Set、Map个Properties的集合属性值。


package Injection;

import java.util.*;

import DependencyInjection.Axe;

public class SetValue {

	private List<String> schools;
	private Map scores;
	private Map<String ,Axe> phaseAxes;
	private Properties health;
	private Set axes;
	private String[] books;
	
	public SetValue() {
		System.out.println("Spring实例化主调bean:SetValue实例..");
	}
	public void test(){
		System.out.println(schools);
		System.out.println(scores);
		System.out.println(phaseAxes);
		System.out.println(health);
		System.out.println(axes);
		System.out.println((java.util.Arrays.toString(books)));
	}
	public void setSchools(List<String> schools) {
		this.schools = schools;
	}
	public void setScores(Map scores) {
		this.scores = scores;
	}
	public void setPhaseAxes(Map<String, Axe> phaseAxes) {
		this.phaseAxes = phaseAxes;
	}
	public void setHealth(Properties health) {
		this.health = health;
	}
	public void setAxes(Set axes) {
		this.axes = axes;
	}
	public void setBooks(String[] books) {
		this.books = books;
	}
	
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>
	<bean id="stoneAxe" class="DependencyInjection.StoneAxe"/>
	<bean id="steelAxe" class="DependencyInjection.SteelAxe"/>
	
	<!-- 集合值注入 -->
	<bean id="setValue" class="Injection.SetValue">
		<property name="schools">
			<!-- 为List属性配置属性值 -->
			<list>
				<value>小学</value>
				<value>中学</value>
				<value>大学</value>
			</list>
		</property>
		<property name="scores">
			<!-- 为Map属性配置属性值 -->
			<map>
				<entry key="数学" value="87"/>
				<entry key="语文" value="89"/>
				<entry key="英语" value="82"/>
				
				<!-- 传统臃肿写法
				<entry>
					<key>
						<value>数学</value>
					</key>
					<value>87</value>
				</entry>
				<entry>
					<key>
						<value>语文</value>
					</key>
					<value>89</value>
				</entry> -->
				
			</map>
		</property>
		<property name="phaseAxes">
			<!-- 为Map属性配置属性值 -->
			<map>
				<entry key="原始社会" value-ref="stoneAxe"/>
				<entry key="农业社会" value-ref="steelAxe"/>
			</map>
		</property>
		<property name="health">
			<!-- 为Properties属性配置属性值 -->
			<props>
				<prop key="血压">正常</prop>
				<prop key="身高">175</prop>
			</props>
		</property>
		<property name="axes">
			<!-- 为Set属性配置属性值 -->
			<set>
				<value>普通的字符串</value>
				<bean class="DependencyInjection.SteelAxe"/>
				<ref bean="stoneAxe"/>		
			</set>
		</property>
		<property name="books">
			<!-- 为数组属性配置属性值 -->
			<list>
				<value>Java程序</value>
				<value>JavaWeb</value>
				<value>Spring框架</value>
			</list>
		</property>
	</bean>
	
</beans>

package Injection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		
		SetValue sv = ctx.getBean("setValue",SetValue.class);
		sv.test();
	}

}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值