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.

    commons-logging-1.1.1.jar
    spring-beans-3.2.9.RELEASE.jar
    spring-core-3.2.9.RELEASE.jar
    spring-context-3.2.9.RELEASE.jar
    spring-expression-3.2.9.RELEASE.jar

  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

Employee.java

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)
    {
        this.id=id;
        this.name=name;
        this.city=city;
    }
    
    public void getEmployeeDetails()
    {
        System.out.println("**Employee Details**");
        System.out.println("ID : "+id);
        System.out.println("Name : "+name);
        System.out.println("City : "+city);
    }
}

SpringConfig.xml

<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-3.0.xsd">
 
    <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"/>
    </bean>
</beans>
  • 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"/>

ClientLogic.java

  • 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
        employee.getEmployeeDetails();
    }
}

Output

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 *