Spring中List、Set、Map、数组注入方式中标签的使用
来源:互联网 发布:网络信息推广发布 编辑:程序博客网 时间:2024/05/07 20:06
在这里不多说,直接进入正题,在使用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/beanshttp://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】
1 0
- Spring中List、Set、Map、数组注入方式中标签的使用
- Spring中数组,List,Map,Set注入方式
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中注入List Set Map Properties的配置文件
- set,list,Map,数组的注入
- Spring中数组、List、Set数据的注入
- spring注入list set map
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring中List Set Map 和Date注入
- spring 配置文件中如何注入map list set等类型
- spring的list,set,map的依赖注入
- C++超基础:STL
- android - NDK 小tips
- 宁夏哪里有找富婆包养男人?
- poj-2479 Maximum sum
- Vivado IP核剖析
- Spring中List、Set、Map、数组注入方式中标签的使用
- 宁波哪里有找富婆包养男人?
- Apache和Nginx 重写服务器规则
- Lectra.Prospinvarsalis.v2R2C1-ISO 1CD
- Lectra.Pro.Style.v5r3c1 1CD
- Django开发中整合新浪微博API
- IT行业智力测试题
- Java集合框架之List公共方法
- http://qinxuye.me/article/third-party-authentication-in-django/