• 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);
        }
    }








  • 相关阅读:
    Java Proxy和CGLIB动态代理原理
    Nginx限速模块初探
    200行Java代码搞定计算器程序
    深入理解Java内置锁和显式锁
    使用linux perf工具生成java程序火焰图
    《深入理解Java函数式编程》系列文章
    Java原子变量
    Java Stream API性能测试
    深入理解Java Stream流水线
    对于web前端基础学习的复习总结
  • 原文地址:https://www.cnblogs.com/meet/p/4758191.html
Copyright © 2020-2023  润新知