Spring中集合的注入方式

1、List

package com.example.demo.test;

import java.util.List;

public class HelloWorld {

	private List msg;

	public List getMsg() {
		return msg;
	}

	public void setMsg(List msg) {
		this.msg = msg;
	}

}

配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld">
		<property name="msg">
			<list>
				<value>data1</value>
				<value>data2</value>
				<value>data3</value>
			</list>
		</property>
	</bean>
</beans>

2、set

实体类类型跟着换一样,就不贴出来了

配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld">
		<property name="msg">
			<set>
				<value>data1</value>
				<value>data2</value>
				<value>data3</value>
			</set>
		</property>
	</bean>
</beans>

3、Map

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld">
		<property name="msg">
			<map>
				<entry key="key1">
					<value>data1</value>
				</entry>
				<entry key="key2">
					<value>data2</value>
				</entry>
				<entry key="key3">
					<value>data3</value>
				</entry>
			</map>
		</property>
	</bean>
</beans>


猜你喜欢

转载自blog.csdn.net/u010142437/article/details/80887427
今日推荐