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








  • 相关阅读:
    shell 知识点
    辅助字符串处理类:org.apache.commons.lang3.StringUtils
    post请求(headers里有属性)报错:Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response
    vue-cli 打包报错:Unexpected token: punc (()
    遍历对象,并对其中第一个(随机)进行处理
    JavaScript中类似PHP的uniqid()方法
    使用crypto-js的md5加密
    Yarn、MapReduce、spark、storm的关系
    hadoop 知识点
    spring cloud 知识点
  • 原文地址:https://www.cnblogs.com/meet/p/4758191.html
Copyright © 2011-2022 走看看