zoukankan      html  css  js  c++  java
  • SpringXML方式配置bean的集合注入:list,map,properties

    新建一个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);
        }
    }








  • 相关阅读:
    bash八大扩展一网打尽
    MySQL命令行导出数据库
    Windows 7上的DirectX 11.1
    把KlayGE嵌入其他GUI框架
    KlayGE的资源载入系统
    学习路漫漫……
    写下我的第一篇Post,呵呵
    今天学习:CSS中的类class和标识id选择符(.和#号)
    Remove Duplicates from Unsorted List
    2012 TODO List
  • 原文地址:https://www.cnblogs.com/meet/p/4758191.html
Copyright © 2011-2022 走看看