/java/

Dependency Injection with the Spring Framework. Setter Injection vs. Constructor Injection

2015-02-11 23:19:34

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

Example

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" http:="" schema="" spring-beans.xsd&quot;="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<bean class='"pl.btbw.BeanA"' id='"beanA"'>
<constructor-arg>
<bean class='"pl.btbw.InBean"/'>
</bean></constructor-arg>
</bean>
<bean class='"pl.btbw.BeanB"' id='"beanB"'>
<property name='"inBean"'>
<bean class='"pl.btbw.InBean"/'>
</bean></property>
</bean>
</beans>

package pl.btbw;

public class BeanA {

    private InBean inBean;

    public BeanA(InBean inBean) {
        this.inBean = inBean;
    }

    public InBean getInBean() {
        return inBean;
    }

    @Override
    public String toString() {
        return "BeanA description";
    }
}
package pl.btbw;

public class BeanB {

    private InBean inBean;

    public void setInBean(InBean inBean) {
        this.inBean = inBean;
    }

    public InBean getInBean() {
        return inBean;
    }

    @Override
    public String toString() {
        return "BeanB description";
    }
}
package pl.btbw;

public class InBean {

    @Override
    public String toString() {
        return "InBean description";
    }
}

How to test

package pl.btbw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        BeanA beanA = context.getBean("beanA", BeanA.class);
        System.out.println(beanA);
        System.out.println(beanA.getInBean());

        BeanB beanB = context.getBean("beanB", BeanB.class);
        System.out.println(beanB);
        System.out.println(beanB.getInBean());
    }
}

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>spring-bean-demo</artifactid>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>commons-logging</groupid>
<artifactid>commons-logging</artifactid>
<version>1.1.3</version>
</dependency>
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>