首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 集合 (List, Set, Map, and Properties) 使用示例

Spring 集合 (List, Set, Map, and Properties) 使用示例

作者头像
九州暮云
发布2019-08-21 14:23:04
7780
发布2019-08-21 14:23:04
举报
文章被收录于专栏:九州牧云九州牧云

记忆里的东西往往容易遗忘,还是写下来放在一个地方比较合适。———— 题记

Spring支持将值注入到四种集合类型当中,这四种集合类型分别是:

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans

以下是一个有四个集合属性的Customer类:

package com.mkyong.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;

	//...
}

接下来我们看看如何在XML中声明这几种集合。

1. List 集合

<property name="lists">
	<list>
		<value>1</value>
		<ref bean="PersonBean" />
		<bean class="com.mkyong.common.Person">
			<property name="name" value="mkyongList" />
			<property name="address" value="address" />
			<property name="age" value="28" />
		</bean>
	</list>
</property>

2. Set 集合

<property name="sets">
	<set>
		<value>1</value>
		<ref bean="PersonBean" />
		<bean class="com.mkyong.common.Person">
			<property name="name" value="mkyongSet" />
			<property name="address" value="address" />
			<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.mkyong.common.Person">
				<property name="name" value="mkyongMap" />
				<property name="address" value="address" />
				<property name="age" value="28" />
			</bean>
		</entry>
	</map>
</property>

4、Properties集合

<property name="pros">
	<props>
		<prop key="admin">admin@nospam.com</prop>
		<prop key="support">support@nospam.com</prop>
	</props>
</property>

##5、完整的配置

<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.mkyong.common.Customer">

		<!-- java.util.List -->
		<property name="lists">
			<list>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.mkyong.common.Person">
					<property name="name" value="mkyongList" />
					<property name="address" value="address" />
					<property name="age" value="28" />
				</bean>
			</list>
		</property>

		<!-- java.util.Set -->
		<property name="sets">
			<set>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.mkyong.common.Person">
					<property name="name" value="mkyongSet" />
					<property name="address" value="address" />
					<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.mkyong.common.Person">
						<property name="name" value="mkyongMap" />
						<property name="address" value="address" />
						<property name="age" value="28" />
					</bean>
				</entry>
			</map>
		</property>

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

	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>

</beans>

运行一下:

package com.mkyong.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("SpringBeans.xml");

    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);

    }
}

输出:

Customer [

lists=[
1,
Person [address=address 1, age=28, name=mkyong1],
Person [address=address, age=28, name=mkyongList]
],

maps={
key 1=1,
key 2=Person [address=address 1, age=28, name=mkyong1],
key 3=Person [address=address, age=28, name=mkyongMap]
},

pros={admin=admin@nospam.com, support=support@nospam.com},

sets=[
1,
Person [address=address 1, age=28, name=mkyong1],
Person [address=address, age=28, name=mkyongSet]]
]

代码下载地址:http://www.mkyong.com/wp-content/uploads/2010/03/Spring-Collection-Example.zip

翻译自:https://www.mkyong.com/spring/spring-collections-list-set-map-and-properties-example/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring beans
  • 1. List 集合
  • 2. Set 集合
  • 3. Map 集合
  • 4、Properties集合
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档