1. 程式人生 > >Spring 集合注入

Spring 集合注入

在Spring中支援注入 陣列、List、Map、Set、Properties

 

注入陣列

package com.javaketang.test;

import java.util.Arrays;

public class Data {

	private String[] arry;

	public String[] getArry() {
		return arry;
	}

	public void setArry(String[] arry) {
		this.arry = arry;
	}

	@Override
	public String toString() {
		return "data:" + Arrays.toString(this.arry);
	}

}

編寫配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="data" class="com.javaketang.test.Data">
		<property name="arry">
			<array value-type="java.lang.String">
				<value>張三</value>
				<value>李四</value>
			</array>
		</property>
	</bean>



</beans>

編寫測試類

public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		Data data=ap.getBean("data",Data.class);

		System.out.println(data);
	}

執行,將會輸出    data:[張三, 李四]

 

注入list集合

package com.javaketang.test;

import java.util.List;

public class Data {

	private List<String> list;

	public List<String> getList() {
		return list;
	}

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

	@Override
	public String toString() {
		return "list:" + list;
	}
}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="data" class="com.javaketang.test.Data">
		<property name="list">
			<list value-type="java.lang.String">
				<value>張三</value>
				<value>李四</value>
			</list>
		</property>
	</bean>



</beans>

測試類

public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		Data data=ap.getBean("data",Data.class);

		System.out.println(data);
	}

執行後將會輸出      list:[張三, 李四]

注入Map集合

package com.javaketang.test;

import java.util.Map;

public class Data {

	private Map<String, Object> map;

	public Map<String, Object> getMap() {
		return map;
	}

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

	@Override
	public String toString() {
		return "map:" + map;
	}
}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="data" class="com.javaketang.test.Data">
		<property name="map">
			<map key-type="java.lang.String" value-type="java.lang.Object">
				<entry key="1" value="李四"></entry>
				<entry key="2" value="張三"></entry>
			</map>
		</property>
	</bean>



</beans>

測試類

	public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		Data data=ap.getBean("data",Data.class);

		System.out.println(data);
	}

執行後將會輸出   map:{1=李四, 2=張三}

 

Set集合注入

package com.javaketang.test;

import java.util.Set;

public class Data {

	private Set<String> set;

	public Set<String> getSet() {
		return set;
	}

	public void setSet(Set<String> set) {
		this.set = set;
	}

	@Override
	public String toString() {
		return "set:" + set;
	}
}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="data" class="com.javaketang.test.Data">
		<property name="set">
			<set value-type="java.lang.String">
				<value>張三</value>
				<value>李四</value>
			</set>
		</property>
	</bean>



</beans>

測試類

public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		Data data=ap.getBean("data",Data.class);

		System.out.println(data);
	}

執行後將會輸出    set:[張三, 李四]

 

Properties注入

在編寫配置檔案Properties型別是最常見的。

package com.javaketang.test;

import java.util.Properties;

public class Data {

	private Properties prop;

	public Properties getProp() {
		return prop;
	}

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

	@Override
	public String toString() {
		return "prop:" + prop;
	}
}

配置檔案

<?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:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd">




	<bean id="data" class="com.javaketang.test.Data">
		<property name="prop">
			<props>
				<prop key="1">張三</prop>
				<prop key="2">李四</prop>
			</props>
		</property>
	</bean>



</beans>

測試類


	public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");

		Data data=ap.getBean("data",Data.class);

		System.out.println(data);
	}

執行後將會輸出    prop:{2=李四, 1=張三}

 

中智軟體科技學校