Spring 专题
专题目录
您的位置:java > Spring专题 > Spring集合 (List,Set,Map,Properties) 实例
Spring集合 (List,Set,Map,Properties) 实例
作者:--    发布时间:2019-11-20
下面例子向您展示spring如何注入值到集合类型(list, set, map, and properties)。 支持4个主要的集合类型:
  • list – <list/>
  • set – <set/>
  • map – <map/>
  • properties – <props/>

spring beans

一个customer对象,有四个集合属性。
package com.h3.common;

import java.util.list;
import java.util.map;
import java.util.properties;
import java.util.set;

public class customer 
{
	private list<object> lists;
	private set<object> sets;
	private map<object, object> maps;
	private properties pros;
	
	//...
}
在bean配置文件中不同的代码片段用来声明集合。

1. list示例

<property name="lists">
		<list>
			<value>1</value>
			<ref bean="personbean" />
			<bean class="com.h3.common.person">
				<property name="name" value="h3list" />
				<property name="address" value="hainan" />
				<property name="age" value="28" />
			</bean>
		</list>
	</property>

2. set示例

<property name="sets">
		<set>
			<value>1</value>
			<ref bean="personbean" />
			<bean class="com.h3.common.person">
				<property name="name" value="h3set" />
				<property name="address" value="hainan" />
				<property name="age" value="28" />
			</bean>
		</set>
	</property>

3. map示例

<property name="maps">
		<map>
			<entry key="key 1" value="1" />
			<entry key="key 2" value-ref="personbean" />
			<entry key="key 3">
				<bean class="com.h3.common.person">
					<property name="name" value="h3map" />
					<property name="address" value="hainan" />
					<property name="age" value="28" />
				</bean>
			</entry>
		</map>
	</property>

4. properties示例

<property name="pros">
		<props>
			<prop key="admin">admin@h3.com</prop>
			<prop key="support">support@h3.com</prop>
		</props>
	</property>
spring完整的 bean 配置文件。
<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-2.5.xsd">

	<bean id="customerbean" class="com.h3.common.customer">

		<!-- java.util.list -->
		<property name="lists">
			<list>
				<value>1</value>
				<ref bean="personbean" />
				<bean class="com.h3.common.person">
					<property name="name" value="h3list" />
					<property name="address" value="hainan haikou" />
					<property name="age" value="28" />
				</bean>
			</list>
		</property>

		<!-- java.util.set -->
		<property name="sets">
			<set>
				<value>1</value>
				<ref bean="personbean" />
				<bean class="com.h3.common.person">
					<property name="name" value="h3set" />
					<property name="address" value="hainan haikou" />
					<property name="age" value="28" />
				</bean>
			</set>
		</property>

		<!-- java.util.map -->
		<property name="maps">
			<map>
				<entry key="key 1" value="1" />
				<entry key="key 2" value-ref="personbean" />
				<entry key="key 3">
					<bean class="com.h3.common.person">
						<property name="name" value="h3map" />
						<property name="address" value="hainan haikou" />
						<property name="age" value="28" />
					</bean>
				</entry>
			</map>
		</property>

		<!-- java.util.properties -->
		<property name="pros">
			<props>
				<prop key="admin">admin@h3.com</prop>
				<prop key="support">support@h3.com</prop>
			</props>
		</property>

	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h31" />
		<property name="address" value="hainan haikou 1" />
		<property name="age" value="28" />
	</bean>

</beans>

执行程序

package com.h3.common;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class app 
{
    public static void main( string[] args )
    {
    	applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");

    	customer cust = (customer)context.getbean("customerbean");
    	system.out.println(cust);
    	
    }
}

输出

customer [lists=[1, com.h3.common.person@4e4ee70bcom.h3.common.person@1e1867d2], sets=[1, com.h3.common.person@4e4ee70bcom.h3.common.person@52f644b4], maps={key 1=1, key 2=com.h3.common.person@4e4ee70b, key 3=com.h3.common.person@54481b6d}, pros={admin=admin@h3.com, support=support@h3.com}]

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册