Spring 的複雜類型的屬性注入:數組,集合(List,Set,Map)

Spring 的複雜類型的屬性注入

1、數組屬性的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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">
	<!-- Spring的屬性注入的方式 -->
	<!-- Spring數組屬性的注入 -->
	<!-- 注入數組類型 -->
	<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
		<!-- 注入數組類型 -->
		<property name="arrs">
			<list>
				<value>王西</value>
				<value>趙娜</value>
				<value>李武</value>
			</list>
		</property>
	</bean>
</beans>

設置對應的屬性和set方法

package com.itzheng.spring.demo5;
import java.util.Arrays;
/*
 * 集合屬性的注入:
 */
public class CollectionBean {
	private String[] arrs;
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	@Override
	public String toString() {
		return "CollectionBean [arrs=" + Arrays.toString(arrs) + "]";
	}
}
package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 複雜類型的屬性注入
 */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

在這裏插入圖片描述

2、集合屬性的注入

(1) list集合當中放入普通類型的value標籤,對象ref

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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">
	<!-- Spring數組屬性的注入 -->
	<!-- 注入數組類型 -->
	<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
		<!-- 注入數組類型 -->
		<property name="arrs">
			<list>
				<value>王西</value>
				<value>趙娜</value>
				<value>李武</value>
			</list>
		</property>
		<!-- 注入list集合 -->
		<property name="list">
			<list>
				<value>張三</value>
				<value>李四</value>
				<value>登峯</value>
			</list>
		</property>
	</bean>
</beans>

在這裏插入圖片描述
設置對應的屬性和set方法

package com.itzheng.spring.demo5;
import java.util.Arrays;
import java.util.List;
/*
 * 集合屬性的注入:
 */
public class CollectionBean {
	private String[] arrs;
	private List<String> list;
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	@Override
	public String toString() {
		return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + "]";
	}
}
package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 複雜類型的屬性注入
 */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

在這裏插入圖片描述

(2)Set集合和Map集合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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">
	<!-- Spring數組屬性的注入 -->
	<!-- 注入數組類型 -->
	<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
		<!-- 注入數組類型 -->
		<property name="arrs">
			<list>
				<value>王西</value>
				<value>趙娜</value>
				<value>李武</value>
			</list>	
		</property>
		<!-- 注入list集合 -->
		<property name="list">
			<list>		
				<value>張三</value>
				<value>李四</value>
				<value>登峯</value>	
			</list>
		</property>
		<!-- 注入set集合 -->
		<property name="set">	
			<set>	
				<value>aaa</value>
				<value>bbb</value>
				<value>ccc</value>
				<value>ddd</value>
			</set>
		</property>
		<property name="map">		
			<map>	
				<entry key="111" value="iiii"></entry>
				<entry key="222" value="zzz"></entry>
				<entry key="333" value="bbb"></entry>
				<entry key="444" value="ddd"></entry>	
			</map>
		</property>
	</bean>
</beans>

在這裏插入圖片描述
設置對應的屬性和set方法

package com.itzheng.spring.demo5;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
 * 集合屬性的注入:
 */
public class CollectionBean {
	private String[] arrs;
	private List<String> list;
	private Set<String> set;
	private Map<String, String> map;
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	@Override
	public String toString() {
		return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
				+ "]";
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章