Spring之集合DI

如果一个类的成员变量为集合类型,而且这些集合需要外部提供,则需要进行DI处理,但是,集合的DI,只能通过setter注入,所以,意味着必须有默认构造函数或者存在构造函数DI。四大集合的代码如下:

package com.bean.DI2;

import java.util.List;

public class ListDI {
	private List<Integer> list;
	public ListDI() {
		// TODO Auto-generated constructor stub
	}
	public void setList(List<Integer> list) {
		this.list = list;
	}
	
	public void print(){
		System.out.println("list di:");
		for(Integer i:list){
			System.out.println(i);
		}
	}
}
package com.bean.DI2;

import java.util.Set;

public class SetDI {
	private Set<Integer> set;
	public SetDI() {
		// TODO Auto-generated constructor stub
	}
	public void setSet(Set<Integer> set) {
		this.set = set;
	}
	public void print(){
		System.out.println("set di:");
		for(Integer i:set){
			System.out.println(i);
		}
	}
}

package com.bean.DI2;

import java.util.Map;

public class MapDI {
	private Map<String, String> map;
	
	public MapDI() {
		// TODO Auto-generated constructor stub
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}


	public void print(){
		System.out.println("map di");
		for(String key:map.keySet()){
			System.out.println("key="+key+",value="+map.get(key));
		}
	}
}

package com.bean.DI2;

import java.util.Properties;

public class PropertyDI {
	private Properties properties;
	public PropertyDI() {
		// TODO Auto-generated constructor stub
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
	public void print(){
		System.out.println("property di:");
		System.out.println(properties);
	}
}

package com.bean.DI2;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
		public static void main(String[] args) {
			
			FileSystemXmlApplicationContext context2=new FileSystemXmlApplicationContext("BeanXml\\DI3.xml");
			SetDI set=(SetDI)context2.getBean("setdi");
			set.print();
			ListDI listDI=(ListDI)context2.getBean("listdi");
			listDI.print();
			MapDI mapDI=(MapDI)context2.getBean("mapdi");
			mapDI.print();
			PropertyDI propertyDI=(PropertyDI)context2.getBean("propertydi");
			propertyDI.print();
			context2.close();
		}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


	<bean id="setdi" class="com.bean.DI2.SetDI">
		<property name="set">
			<set>
				<value>15</value>
				<value>16</value>
				<value>17</value>
			</set>
		</property>
	</bean>
	
	<bean id="listdi" class="com.bean.DI2.ListDI">
		<property name="list">
			<list>
				<value>5</value>
				<value>6</value>
				<value>7</value>
			</list>
		</property>
	</bean>
	
	<bean id="mapdi" class="com.bean.DI2.MapDI">
		<property name="map">
			<map>
				<entry key="one" value="1"></entry>
				<entry key="two" value="2"></entry>
				<entry key="three" value="3"></entry>
			</map>
		</property>
	</bean>
	
	<bean id="propertydi" class="com.bean.DI2.PropertyDI">
		<property name="properties">
			<props>
				<prop key="4">four</prop>
				<prop key="5">five</prop>
				<prop key="6">six</prop>
			</props>
		</property>
	</bean>
</beans>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值