spring集合注入

集合注入方式如下:
package com.dp.service;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class UnionService {
	
	public List<String> list;
	
	public Map<String, String> map;
	
	public Set<String> set;
	
	public Properties prop;

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

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

	public void setSet(Set<String> set) {
		this.set = set;
	}
	
	public void unionDITest(){
		//list
		System.out.println("list DI result:");
		for(String strlist:list){
			System.out.println(strlist);
		}
		//set
		System.out.println("set DI result:");
		for(String strset:set){
			System.out.println(strset);
		}
		//map
		System.out.println("map DI result:");
		for(String strMap:map.keySet()){
			System.out.println(strMap + " = " +map.get(strMap));
		}
		//prop
		System.out.println("prop DI result: ");
		for(Object strProp:prop.keySet()){
			System.out.println(strProp + " = " + prop.getProperty((String) strProp));
		}
		
	}

}


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="unionService" class="com.dp.service.UnionService">
		<property name="list">
			<list>
				<value>list1</value>
				<value>list2</value>
				<value>list3</value>
			</list>
		</property>
		<property name="set">
			<set>
				<value>set1</value>
				<value>set2</value>
				<value>set3</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry key="mapkey1" value="mapvalue1"></entry>
				<entry key="mapkey2" value="mapvalue2"></entry>
				<entry key="mapkey3" value="mapvalue3"></entry>
			</map>
		</property>
		<property name="prop">
			<props>
				<prop key="propkey1">propvalue1</prop>
				<prop key="propkey2">propvalue2</prop>
				<prop key="propkey3">propvalue3</prop>
			</props>
		</property>
		
	</bean>

</beans>


Java代码
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(XML_EXPRESSION);
		UnionService unionService = (UnionService) applicationContext.getBean("unionService");
		unionService.unionDITest();

你可能感兴趣的:(spring)