Today we are going to understand about one of the main features of Spring Framework - Dependency Injection
and how to use this feature within a Spring Project using the Eclipse Integrated Development Environment(IDE) application.
Dependency Injection is a feature of Spring Framework which allows us inject primitive values as well as an object(bean) value into another object(bean).
In dependency injection, a dependency could be a primitive value or an object(bean).
An injection is the passing of a such dependency(primitive value or an object) to a dependent object that would use it.
dependency injection can be performed in two ways -
By setter methods
To understand this concept of dependency Injection by the setter method, let's proceed with an example.
In the upcoming section, we are going to create a few loosely coupled Java classes also known as beans, configure these
beans in a configuration xml file, load and instantiate the beans and perform dependency injection using the Spring Framework.
Creating the Java class - OutdoorSport
We are going to create a java class named OutdoorSport within the decodejava package and this class contains a message info().
public class OutdoorSport
public void info()
System.out.println("-An Outdoor Sport-");
Dependent class with an OutdoorSport object - Tennis
Next, we are going to add another Java class named Tennis and it is
going to contain a String object and an OutdoorSport reference, hence Tennis class is dependent on OutdoorSport class.
These objects of String class and the OutdoorSport class will be injected into a Tennis instance/bean by an automatic call to the setter methods in the Tennis class
by the Spring Container, when
the Tennis bean is created by it using the configuration xml file(to be created in the upcoming section).
public class Tennis
public void getMessage()
System.out.println("Message is : " + message);
//Setter method to set the message
public void setMessage(String message)
this.message = message;
//Setter method to set the OutdoorSport object
public void setSport(OutdoorSport sport)
this.sport = sport;
Adding the Utility class that calls the Spring API
Next, we are going to create another class named - Utility, which is a simple java class.
The Utility class uses the ApplicationContextcontainer(an interface) of Spring Framework by
creating its instance using its implemented class FileSystemXmlApplicationContext, which -
Loads the configuration xml file - config.beans.xml
Creates the Tennis bean, calls setter methods i.e. setMessage() & setSport(), creates the OutdoorSport bean specified in it and injects it into Tennis bean.
Accesses the bean specified in the configuration file by calling the getBean() method of ApplicationContextcontainer.
Calls the method of Tennis class - getMessage().
public class Utility
public static void main(String args)
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Tennis tennisBean = context.getBean("TennisBean", Tennis.class);
Adding a configuration file
Next, we are going to add a configuration file to our project.
This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension and
we are going to name file as config.beans.xml.
In this file, we have configured a Tennis instance with a unique id and a String object and an OutdoorSport
instance is injected into the Tennis bean using the setter methods in Tennis class..
<?xml version="1.0" encoding="utf-8"?>
<bean id ="TennisBean" class="decodejava.Tennis">
<property name="message" value="Let's play Tennis!"></property>
<property name="sport" ref="OutdoorSportBean"></property>
<bean id ="OutdoorSportBean" class="decodejava.OutdoorSport">
This mapping document has a parent <beans> tag as the root element and its individual child elements, each with a <bean> tag, containing all the attributes such as -
The id attribute within the <bean> element defines a unique id associated with the bean.
The class attribute within the <bean> element defines the name of the class of this bean.
The property child element in <bean> tag refers to a property within the Tennis class and it has some attributes -
The name child attribute refers to the name of a property in the Tennis class i.e. sport and message.
The value child attribute refers to the value of a property in the Tennis class i.e. property
message, is automatically assigned a value Let's play Tennis! by calling the setter method - setMessage(), when the bean is created by Spring Container.
The ref child attribute refers to the reference name of the property which is matched with the id
of another bean(another class - OutdoorSport) declared within a different <bean> tag. The leads to calling of setter method - setSport() by the Spring container.
We are going to add some JARs files to our Java project.
These JARs are required in order to successfully execute a Spring project.
All these JARs are included in the folder named libs(within our Spring installation folder).
So, we need to add all the JARs in the libs folder to our build path of our Java project.
The picture above depicts how and where to arrange classes and interfaces comprising this Spring Project,
in a specific directory structure.
Project Folder - SpringDependencyInjection is the name of our Project and it is a top-level directory.
This project folder contains a package of our project i.e. decodejava
containing three classes -
An POJO/Entity class file i.e. OutdootSport.java.
A dependent class named Tennis.java class, which contains an object of OutdoorSport class.
A class that calls the Spring Framework into action i.e. Utility.java.
Besides this, the project folder also contains a configuration file i.e. config.beans.xml, used to configure the beans of classes in the project.
The project folder also contains all the needed Spring JARs present in the libs folder of Spring Framework
Finally, after executing Utility class, you will get the following output within the Console window.
This output shown below, shows how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml,
access the beans specified in it, instantiate the Tennis class and OutdoorSport class. It also injects OutdoorSport bean into Tennis bean and calls their methods.
Jul 06, 2018 1:02:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Fri Jul 06 13:02:02 IST 2018]; root of context hierarchy
Jul 06, 2018 1:02:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
-An Outdoor Sport-
This concludes performing the dependency injection by injecting a bean into another bean using the setter methods. In the next article, we are
going to show you how to perform the dependency injection using the constructor.