Di --- collections injection


First, a set of injection

	 <bean id="mc" class="com.gql.di.MyCollection">
	 	<property name="list">
	 		<list>
	 			<value>13</value>
	 			<value>14</value>
	 		</list>
	 	</property>
	 	
	 	<property name="set">
	 		<set>
	 			<value>21</value>
	 			<value>22</value>
	 		</set>
	 	</property>
	 	
	 	<property name="map">
	 		<map>
	 			<entry key="1" value="gql"></entry>	
	 			<entry key="2" value="zdy"></entry>	
	 		</map>
	 	</property>
	 		 	
	 	<property name="prop">
	 		<props>
	 			<prop key="name" >Tom</prop>
	 			<prop key="name" >Jon</prop>	 			
	 		</props>
	 	</property>

Successfully injected through a collection of objects.
Here Insert Picture Description

(1) the set of tags

  • For a List结构set of injection listtags: array, set, .
  • For a Map结构set of injection maptags: props, .
  • The same configuration, the label may be interchanged .

(2) injecting the test set

package com.gql.di;
import javax.swing.plaf.synth.SynthSpinnerUI;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gql.entity.Person;

/**
 * 类说明:
 *		测试集合注入
 * @guoqianliang1998.
 */
public class Demo {

	@Test
	public void testCollection(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		MyCollection mc = (MyCollection) ac.getBean("mc");
		//打印List集合
		System.out.println("List集合:"+mc.getList());
		//打印Set集合
		System.out.println("Set集合:"+mc.getSet());
		//打印Map集合
		System.out.println("Map集合:"+mc.getMap());
		//打印Prop集合
		System.out.println("Prop集合:"+mc.getProp());
	}
}

(3) MyCollection entity class

package com.gql.di;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 类说明:
 *		集合实体类
 * @guoqianliang1998.
 */
public class MyCollection {
	private List list;
	private Set set;
	private Map map;
	private Properties prop;
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Set getSet() {
		return set;
	}
	public void setSet(Set set) {
		this.set = set;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
}

(4) XML arranged

applicationContext_Di.xml (partial configuration)

<?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="mc" class="com.gql.di.MyCollection">
	 
	 	<property name="list">
	 		<list>
	 			<value>13</value>
	 			<value>14</value>
	 		</list>
	 	</property>
	 	
	 	<property name="set">
	 		<set>
	 			<value>21</value>
	 			<value>22</value>
	 		</set>
	 	</property>
	 	
	 	<property name="map">
	 		<map>
	 			<entry key="1" value="gql"></entry>	
	 			<entry key="2" value="zdy"></entry>	
	 		</map>
	 	</property>
	 		 	
	 	<property name="prop">
	 		<props>
	 			<prop key="name" >Tom</prop>
	 			<prop key="name" >Jon</prop>	 			
	 		</props>
	 	</property>
	 		 	
	 </bean> 
</beans>

the applicationContext.xml (Global configuration)

<?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">

	<import resource="com/gql/di/applicationContext_Di.xml"/>
</beans>
Published 365 original articles · won praise 973 · views 170 000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/104096832