Example Spring 4 – Dependency Injection – Constructor Injection

spring example

This example presents the basic concept of using constructor injection in Spring.

The technologies used are :
– Spring 4.3.4
– JDK 1.8
– Maven 3.3.9

You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :

mvn eclipse:eclipse
package javaspringexamples.spring.ioc.constructorInjection.xml;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class A {
	private B b;
	private C c;
	
	public A(B b, C c) {
		this.b = b;
		this.c = c;
	}

	public A(C c, B b) {
		this.b = b;
		this.c = c;
	}

	@Override
	public String toString() {
		return b.toString() + "-" + c.toString();
	}

}
package javaspringexamples.spring.ioc.constructorInjection.xml;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class B {
	private String name;

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

	@Override
	public String toString() {
		return name;
	}

}
package javaspringexamples.spring.ioc.constructorInjection.xml;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class C {
	private String name;

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

	@Override
	public String toString() {
		return name;
	}
}
package javaspringexamples.spring.ioc.constructorInjection.xml;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class Main {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"/javaspringexamples/spring/ioc/constructorInjection/xml/xmlConfiguration.xml");

		// Getting and printing objectA_BC
		A objectA = (A) applicationContext.getBean("objectA_BC");
		System.out.println(objectA);

		// Getting and printing objectA_BC
		objectA = (A) applicationContext.getBean("objectA_CB");
		System.out.println(objectA);

	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<util:map id="objectName">
		<entry key="A" value="Object A" />
		<entry key="B" value="Object B" />
		<entry key="C" value="Object C" />
	</util:map>

	<bean id="objectA_BC"
		class="javaspringexamples.spring.ioc.constructorInjection.xml.A">
		<constructor-arg ref="objectB" />
		<constructor-arg ref="objectC" />
	</bean>

	<bean id="objectA_CB"
		class="javaspringexamples.spring.ioc.constructorInjection.xml.A">
		<constructor-arg index="1" ref="objectC" />
		<constructor-arg index="0" ref="objectB" />
	</bean>

	<bean id="objectB"
		class="javaspringexamples.spring.ioc.constructorInjection.xml.B">
		<constructor-arg value="#{objectName[B]}" />
	</bean>

	<bean id="objectC"
		class="javaspringexamples.spring.ioc.constructorInjection.xml.C">
		<constructor-arg value="#{objectName[C]}" />
	</bean>
</beans>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>javaspringexamples.spring</groupId>
    <artifactId>example-XML-Based-Constructor-Injection</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>example XML Based Constructor Injection</name>
 
    <properties>
        <springframework.version>4.3.4.RELEASE</springframework.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The output is :
Object B-Object C
Object B-Object C

Get the sources of the example from the following GitHub url

Or Download a .zip file