Recap - Autowiring is a feature provided by Spring Framework which automate the process of wiring the beans in a Spring Project.
To perform autowiring of beans, Spring provides us an attribute -autowire.
Attribute autowire may have any of the three values -
When a class A contains a property/reference of class B and also has a constructor with argument type class B, the use of attribute autowire="constructor"
within the class A bean(configured in xml file) leads the
Spring container to look for the bean(in xml) of type class B(as defined in the constructor argument) and inject it into the class A bean.
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 attribute - autowire
with its value - constructor.
Creating the Java class - Company
We are going to create a java class named Company within the decodejava package and this class contains a message info().
public class Company
public void info()
Dependent Class containing a Company object - Employee
Next, we are going to add another Java class named Employee and it is
going to contain a String object, a primitive int and an Company reference, hence Employee class is dependent on Company class as it contains its reference.
We are also going to create a constructor(a must when using autowire="constructor") of Employee class with argument type Company, String and a primitive int.
The objects of String class and Company class and primitive int value will be injected into a Employee instance/bean by an automatic
call to the constructor of Employee class
by the Spring Container, when
the Employee bean is created by it using the configuration xml file(to be created in the upcoming section).
public class Employee
Company company ;
//Constructor is a must when using autowire="constructor"
public Employee(Company company, String name, int yearsOfExp)
this.company = company;
this.name = name;
this.yearsOfExp = yearsOfExp;
public Company getCompany()
public String getName()
public int getYearsOfExp()
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 and
Creates the Employee bean by calling setter methods i.e. setName(), setCompany() and setYearsOfExp, creates the
specified Company bean
and injects it into Employee bean.
Accesses the Employee bean specified in the configuration file by calling the getBean() method of ApplicationContextcontainer.
Eventually this class calls the method of Employee class - getName() and getYearsOfExp.
public class Utility
public static void main(String args)
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Employee employeeBean = context.getBean("EmployeeBean", Employee.class);
System.out.println("Employee Name : " + employeeBean.getName());
System.out.println("Years of experience : " + employeeBean.getYearsOfExp());
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 an Employee bean with a unique id, a String object, a primitive int value and a Company
bean is injected into the Employee bean, using the autowire="constructor"
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 <constructor-arg> child element in <bean> tag refers to arguments within the constructor of Employee class and it has some attributes -
The index child attribute refers to the index position of the argument in the constructor in the Employee class i.e. 1st index represents the String argument i.e. name and the 2nd index represents
primitive int argument i.e. yearsOfExp.
The value child attribute refers to the value assigned to the argument at the specific index in the
constructor of Employee class, which is automatically called 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 - Company) declared within a different <bean> tag. The leads to creation of Company object.
This Company bean is eventually injected into Employee bean with String and primitive int value 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 - SpringDependencyInjectionAutowireByConstructor 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. Company.java.
A dependent class named Employee.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 Company class and Employee class.
It also injects a String object, a primitive int value and a
Company bean into Employee bean and calls the methods of the respective classes.
Jul 17, 2018 11:21:23 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Tue Jul 17 11:21:23 IST 2018]; root of context hierarchy
Jul 17, 2018 11:21:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Employee Name : Employee1
Years of experience : 7
This concludes performing the dependency injection by injecting a bean into another bean using the autowire attribute value - constructor.
In the next article, we are
going to show you how to perform the dependency injection using the autowiring with annotations.