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

SpringXML方式配置bean的集合注入:list,map,properties

时间:2015-08-25 19:13:44      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

新建一个bean,设置相应的集合属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Collections {
    private Set<String> sets;
    private List<String> lists;
    private Map<String,String> maps;
    public Set<String> getSets() {
        return sets;
    }
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public List<String> getLists() {
        return lists;
    }
    public void setLists(List<String> lists) {
        this.lists = lists;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
}

在配置文件中配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<bean id="coll" class=" com.fz.entity.Collections">
    <property name="sets">
        <set>
            <value>set1</value>
            <value>set2</value>
        </set>
    </property>
    <property name="lists">
        <list>
            <value>list1</value>
            <value>list2</value>
        </list>
    </property>
    <property name="maps">
        <map>
            <entry key="map1" value="map1" />
            <entry key="map2" value="map2" />
            <entry key="map3" value="map3" />
        </map>
    </property>
</bean>

测试获取bean的值,此时控制台打印:list1 和list2

1
2
3
4
5
6
7
8
@Test
public void getProperties(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    Collections coll = (Collections) ctx.getBean("coll");
    for (String temp : coll.getLists()) {
        System.out.println(temp);
    }
}








SpringXML方式配置bean的集合注入:list,map,properties

标签:

原文地址:http://www.cnblogs.com/meet/p/4758191.html

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