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








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