/java/

Dependency Injection with the Spring Framework. Xml solution.

2015-02-11 22:05:36

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

Example 1

simple example, constructor without parameters

package pl.btbw;

public class BeanA {

    public BeanA() {
    }

    @Override
    public String toString() {
        return "BeanA description";
    }
}
<?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"'></bean>
</beans>

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

Example 2

constructor with one String parameter

package pl.btbw;

public class BeanA {

    private String name;

    public BeanA(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "BeanA description for: " + name;
    }
}
<?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 ipsum&quot;="" value='"Lorem'></constructor-arg>
</bean>
</beans>

How to test

use code form example 1

Example 3

constructor with one other Bean parameter

package pl.btbw;

public class BeanA {

    public BeanA(BeanB beanB) {
        System.out.println(beanB.toString());
    }

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

public class BeanB {
    @Override
    public String toString() {
        return "BeanB description";
    }
}
<?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 ref='"beanB"/' type='"pl.btbw.BeanB"'>
</constructor-arg></bean>
<bean class='"pl.btbw.BeanB"/' id='"beanB"'>
</bean></beans>

How to test

use code form example 1

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>