Spring——IOC各种类型的集合注入

首先创建一个类,包含List、array、set、map、properties属性

AllCollectionType.java

public class AllCollectionType {
	private List<String> listCollection;
	private String[] arrayCollection;
	private Set<String> setCollection;
	private Map<String,String> mapCollection;
	private Properties propsCollection;
	
	public List<String> getListCollection() {
		return listCollection;
	}
	public void setListCollection(List<String> listCollection) {
		this.listCollection = listCollection;
	}
	public String[] getArrayCollection() {
		return arrayCollection;
	}
	public void setArrayCollection(String[] arrayCollection) {
		this.arrayCollection = arrayCollection;
	}
	public Set<String> getSetCollection() {
		return setCollection;
	}
	public void setSetCollection(Set<String> setCollection) {
		this.setCollection = setCollection;
	}
	public Map<String, String> getMapCollection() {
		return mapCollection;
	}
	public void setMapCollection(Map<String, String> mapCollection) {
		this.mapCollection = mapCollection;
	}
	public Properties getPropsCollection() {
		return propsCollection;
	}
	public void setPropsCollection(Properties propsCollection) {
		this.propsCollection = propsCollection;
	}
	@Override
	public String toString() {
		return "AllCollectionType [listCollection=" + listCollection + ", arrayCollection="
				+ Arrays.toString(arrayCollection) + ", setCollection=" + setCollection + ", mapCollection="
				+ mapCollection + ", propsCollection=" + propsCollection + "]";
	}

}

applicationContext.xml

<bean id="Collection" class="com.dt.entity.AllCollectionType">
	       
	<property name="listCollection">
			<list>
				<value>苹果1</value>
				<value>菠萝1</value>
				<value>西瓜1</value>
			</list>
	</property>
	
	<property name="arrayCollection">
		<array>
				<value>苹果2</value>
				<value>菠萝2</value>
				<value>西瓜2</value>
		</array>
	</property>
	
	<property name="setCollection">
		<set>
				<value>苹果3</value>
				<value>菠萝3</value>
				<value>西瓜3</value>
		</set>
	</property>
	
	<property name="mapCollection">
		<map>
			<entry>
				<key>
					<value>apple</value>
				</key>
				<value>
				苹果4
				</value>
			</entry>
			<entry>
				<key>
					<value>pineapple </value>
				</key>
				<value>
				菠萝4
				</value>
			</entry>
			<entry>
				<key>
					<value>waterlmeon</value>
				</key>
				<value>
				西瓜4
				</value>
			</entry>
		</map>
	</property>
	<property name="propsCollection">
		<props>
			<prop key="apple5">苹果5</prop>
			<prop key="pineapple5">菠萝5</prop>
			<prop key="watermelon5">西瓜5</prop>
		</props>
	
	</property>
	
	</bean>

测试类test.java

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
collection();

	}
	//测试集合类型
	public static void collection(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		AllCollectionType type =(AllCollectionType)context.getBean("Collection");
		System.out.println(type);
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值