Spring Dependency Injection – Constructor Injection

Through my earlier posts we have learnt about Spring Setter Injection and all its types, in setter injection we will be having the POJO’s for all the property which is present in the class, whereas in Constructor Injection we will have the parameterized constructors which will be setting values to the properties. We will be using <constructor-arg> tag to achieve it.

Folder Structure:

  1. Create a new Java Project  SpringCoreTutorial” and create a package for our src files com.javainterviewpoint
  2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.


  3. Create the Java classes Employee.java and ClientLogic.java under  com.javainterviewpoint folder.
  4. Place our configuration file SpringConfig.xml in the src directory


Our Employee class has three properties (id,name and city) and a parameterized constructor which sets up the values for the properties. The getEmployeeDetails() method prints the employee details which is set through constructor injection.

package com.javainterviewpoint;

public class Employee 
    private int id;
    private String name;
    private String city;
    public Employee(int id, String name, String city)
    public void getEmployeeDetails()
        System.out.println("**Employee Details**");
        System.out.println("ID : "+id);
        System.out.println("Name : "+name);
        System.out.println("City : "+city);


<beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="employee" class="com.javainterviewpoint.Employee">
      <constructor-arg value="10"/>
      <constructor-arg type="java.lang.String" value="JavaInterviewPoint"/>
      <constructor-arg type="java.lang.String" value="Chennai"/>
  • SpringConfig.xml has the bean definitions, We have set bean id as “employee” for our Employee class which will act as the reference for calling our Employee class.
  • Using the <constructor-arg> tag we are setting the values to the properties of the Employee class
  • We can also specify the type of argument which you are passing as well through a additional parameter “type”
    <constructor-arg type="java.lang.String" value="JavaInterviewPoint"/>


  • In our ClientLogic class we will  Read the Configuration file(SpringConfig.xml) and get all the bean definition through BeanFactory
  • Get the Employee Class instance by calling the getBean() method over the bean factory.
  • The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
  • Call the getEmployeeDetails() method to display the values which we injected through the setter.
package com.javainterviewpoint;

import java.util.Enumeration;
import java.util.Properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientLogic 
    public static void main(String args[])
        //Read the configuration file
        Resource resource = new ClassPathResource("SpringConfig.xml");
        //Read all the bean definition
        BeanFactory bf = new XmlBeanFactory(resource);
        //Get the Employee class instance
        Employee employee = (Employee)bf.getBean("employee");
        //Print the employee details


Once we run our ClientLogic.java we will get the below output

**Employee Details**
ID : 10
Name : JavaInterviewPoint
City : Chennai

Leave a Reply

Your email address will not be published. Required fields are marked *