Skip to content

Dependency Injection with the Spring Framework. Xml DI. Collection.

2015-02-15 13:02:35

Yes I know, everybody love @Annotation, but it is good to know how the xml DI works.


Simple example of Dependency Injection for list, map and set.


package pl.btbw;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class BeanCollectionWrapper {

    private List

<string> list;

    private Set<string> set;

    private Map<string, string=""> map;

    public List<string> getList() {
        return list;

    public void setList(List<string> list) {
        this.list = list;

    public Set<string> getSet() {
        return set;

    public void setSet(Set<string> set) {
        this.set = set;

    public Map<string, string=""> getMap() {
        return map;

    public void setMap(Map<string, string=""> map) { = map;


<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans beans="" http:="" schema="" spring-beans.xsd&quot;="""" xmlns='""' xmlns:xsi='""' xsi:schemalocation='"'>
<bean class='"pl.btbw.BeanCollectionWrapper"' id='"beanCollectionWrapper"'>
<property name='"list"'>
<value>l.Value 1</value>
<value>l.Value 2</value>
<value>l.Value 3</value>
<property name='"set"'>
<value>s.Value 1</value>
<value>s.Value 2</value>
<value>s.Value 3</value>
<property name='"map"'>
<entry 1&quot;="" key='"a"' value='"m.Value'></entry>
<entry 2&quot;="" key='"b"' value='"m.Value'></entry>
<entry 3&quot;="" key='"c"' value='"m.Value'></entry>

How to test.


package pl.btbw;

import org.springframework.context.ApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

        BeanCollectionWrapper beanCollectionWrapper
                = context.getBean("beanCollectionWrapper", BeanCollectionWrapper.class);



[l.Value 1, l.Value 2, l.Value 3]
{a=m.Value 1, b=m.Value 2, c=m.Value 3}
[s.Value 1, s.Value 2, s.Value 3]