码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中List、Set、Map、数组注入方式中标签的使用

时间:2015-01-10 21:04:23      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:spring   xml   java   bean   

       在这里不多说,直接进入正题,在使用Spring时,如果对象实例含有数据或集合属性时,那我们该如何去配置Spring.xml呢?我们就需要property下的子元素list,set,map等子元素。示例为:

<bean>
	<property>
		<list>……</list>
		or
		<set>……</set>
		or
		<map>……</map>
	</property>
</bean>

【转载使用,请注明出处:http://blog.csdn.net/mahoking

 

操作步骤:
1、 创建Topic对象。

public class Topic {

	/**内容   必须提供     getter 与 setter 方法*/
	public String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	/**
	 * 有参的构造函数 ,可选
	 * @param context
	 */
	public Topic(String context) {
		this.context = context;
	}
	
	/**
	 * 无参数的构造函数  , 必须提供一个无参的构造函数
	 */
	public Topic() {
	}

2、 创建Speaker对象。

public class Speaker {

	private List<Topic> topicLists;
	private Set<Topic>  topicSets;
	private Map<Integer, Topic> topicMaps;
	private Topic[] topics;
	private String[] topicStrings;
	
	/**
	 * 演讲
	 */
	public void teach() {

		System.out.println("====List====");
		for(Topic topic:topicLists){
			System.out.println(topic.getContext());
		}
		
		System.out.println("====Set====");
		for(Topic topic:topicSets){
			System.out.println(topic.getContext());
		}
		
		System.out.println("====Topic[]====");
		for(Topic topic:topics){
			System.out.println(topic.getContext());
		}
		
		System.out.println("====Map====");
		for(Entry<Integer, Topic> entry:topicMaps.entrySet()){
			System.out.println(topicMaps.get(entry.getKey()).getContext());
		}
		
		System.out.println(topicStrings[0]+"==="+topicStrings[1]);
	}
	
	/*Getter 和  Setter*/
	public List<Topic> getTopicLists() {
		return topicLists;
	}
	public void setTopicLists(List<Topic> topicLists) {
		this.topicLists = topicLists;
	}
	public Set<Topic> getTopicSets() {
		return topicSets;
	}
	public void setTopicSets(Set<Topic> topicSets) {
		this.topicSets = topicSets;
	}
	public Map<Integer, Topic> getTopicMaps() {
		return topicMaps;
	}
	public void setTopicMaps(Map<Integer, Topic> topicMaps) {
		this.topicMaps = topicMaps;
	}
	public Topic[] getTopics() {
		return topics;
	}
	public void setTopics(Topic[] topics) {
		this.topics = topics;
	}
	public String[] getTopicStrings() {
		return topicStrings;
	}
	public void setTopicStrings(String[] topicStrings) {
		this.topicStrings = topicStrings;
	}
}

3、创建Spring配置文件highLearn02.xml。

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Spring中数组,List,Map,Set注入方式 -->
	<bean id="highSpeaker02" class="com.mahaochen.spring.high.learn02.Speaker"
		scope="singleton" >

		<!-- List注入方式 -->
		<property name="topicLists">
			<list>
				<ref bean="highTopic01" />
				<ref bean="highTopic02" />
			</list>
		</property>
		<!-- Set注入方式 -->
		<property name="topicSets">
			<set>
				<ref bean="highTopic01" />
				<ref bean="highTopic02" />
			</set>
		</property>
		<!-- Map注入方式 -->
		<property name="topicMaps">
			<map>
				<entry key="1">
					<ref bean="highTopic01" />
				</entry>
				<entry key="2">
					<ref bean="highTopic02" />
				</entry>
			</map>
		</property>
		<!-- 数组注入方式 -->
		<property name="topics">
			<list>
				<ref bean="highTopic01" />
				<ref bean="highTopic02" />
			</list>
		</property>
		<property name="topicStrings">
			<list>
				<value>Happy</value>
				<value>Smell</value>
			</list>
		</property>
	</bean>

	<bean id="highTopic01" class="com.mahaochen.spring.high.learn02.Topic">
		<constructor-arg index="0" value="Left" />
	</bean>
	<bean id="highTopic02" class="com.mahaochen.spring.high.learn02.Topic">
		<constructor-arg index="0" value="Right" />
	</bean>
</beans>

4、将Spring配置文件highLearn02.xml引入到主配置文件beans.xml中。

<import resource="com/mahaochen/spring/learn03/beanLearn03.xml"/>

5、编写测试类TestSpringHigh02.java。

public class TestSpringHigh02 {

	// Spring中List、Set、Map、数组注入方式
	public static void main(String[] args) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
		
		Speaker speakerHigh02 = (Speaker) beanFactory.getBean("highSpeaker02");
		speakerHigh02.teach();
	}
}



【转载使用,请注明出处:http://blog.csdn.net/mahoking



Spring中List、Set、Map、数组注入方式中标签的使用

标签:spring   xml   java   bean   

原文地址:http://blog.csdn.net/mahoking/article/details/42586999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!