- Details
- Written by Nam Ha Minh
- Last Updated on 24 June 2019   |   Print Email
In this Java Spring tutorial, you will learn how to implement a simple dependency injection example in Spring framework with XML configuration. To understand the core concepts of dependency injection, please refer to the article
What is Dependency Injection with Java Code Example.You know, dependency injection is the corner-stone of Spring framework, so having a good understanding about dependency injection is the first step to get started with Spring - one of the most popular frameworks for building enterprise Java applications.
1. Create Maven Project in Eclipse
In Eclipse, click menu
File > New > Maven Project. In the
New Maven Project dialog, check the option
Create a simple project (skip archetype selection):

Click
Next. Then enter
Group Id and
Artifact Id for the project like this:
Click
Finish, and wait for few seconds while Eclipse is generating the structure for the project.Open the
pom.xml file, and copy - paste the following XML code just before the
</project> closing tag:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
This
spring-context is the minimum dependency for using Spring framework so you can work with its dependency injection feature. Save the file, and you will see Maven automatically downloads the required JAR files as shown below:

Next, create a Java package called
net.codejavaunder
src/main/java folder. And create some Java classes as described in the
What is Dependency Injection with Java Code Example tutorial.The interface
Client:
package net.codejava;
public interface Client {
void doSomething();
} The class
ClientA - an implementation of
Client:
package net.codejava;
public class ClientA implements Client {
Service service;
public ClientA(Service service) {
this.service = service;
}
@Override
public void doSomething() {
String info = service.getInfo();
System.out.println(info);
}
public void setService(Service service) {
this.service = service;
}
} The interface
Service - which is used by
ClientA:
package net.codejava;
public interface Service {
String getInfo();
} The class
ServiceB - an implementation of
Service:
package net.codejava;
public class ServiceB implements Service {
@Override
public String getInfo() {
return "ServiceB's Info";
}
}You see, the
ClientA and
ServiceB classes are independent of each other.
ClientA is not aware the existence of
ServiceB, as it works with only an implementation of
Service. Then the job of Spring framework is to inject an instance of
ServiceB to
ClientA via XML configuration as you will do below.
2. Configure Dependency Injection using XML
Spring framework allows you to configure the dependency among classes using a XML file called application context file. Under the
src/main/resources folder, create a XML file named
applicationContext.xml with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service1" class="net.codejava.ServiceB" />
<bean id="client1" class="net.codejava.ClientA">
<constructor-arg ref="service1" />
</bean>
</beans>Here, an instance of
ServiceB class is declared using the
<bean> tag as follows:
<bean id="service1" class="net.codejava.ServiceB" />
This bean name
service1 is injected to the constructor of an instance of
ClientA as follows:
<bean id="client1" class="net.codejava.ClientA">
<constructor-arg ref="service1" />
</bean>
This is called
constructor injection.You see, the wiring of dependencies is done via XML so the client and service classes are very loosely coupled.The following example shows how to inject the dependency using
setter injection:
<bean id="client1" class="net.codejava.ClientA">
<property name="service" ref="service1" />
</bean>
In this case, the class
ClientA must have an instance field named
service and the no-argument constructor:
public class ClientA implements Client {
Service service;
public ClientA() {
}
...
}If you want to change the
Service’s implementation injected to
ClientA, just edit the XML:
<bean id="service1" class="net.codejava.ServiceC" />
ClientA remains unchanged and is not aware of this change, thus increase the flexibility, independence and reusability of code.
3. Test Spring Dependency Injection
To test what have created so far, create a Java class with main method named
SpringDependencyExample with the following code:
package net.codejava;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDependencyExample {
public static void main(String[] args) {
ApplicationContext appContext
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Client client = (Client) appContext.getBean("client1");
client.doSomething();
}
}In this program, we load the Spring’s application context from the specified XML file
applicationContext.xml, get a bean named
service1 from the context, cast it to
Client type and invoke its method:
Client client = (Client) appContext.getBean("client1");
client.doSomething();The
getBean() method of the
ApplicationContext class is used to retrieve the reference of an instance managed by Spring’s IoC (Inversion of Control) container.That’s basically how to use dependency injection with Spring framework. You use the
<bean> tags to declare the dependencies in a XML file, use the
ClassPathXmlApplicationContext class to load the configuration from the XML file, and call
getBean() method to retrieve a bean instance from the container.We hope you found this tutorial helpful to get started with Spring framework from its core concept - dependency injection.
References:
Related Spring Dependency Injection Tutorials:
Other Spring Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on
Facebook and watch
his Java videos on YouTube.
Add comment
Comments