×
☰ See All Chapters

Constructor Injection in Spring

The constructor injection is the method of injecting the dependencies of an object through its constructor arguments.  Since constructor invokes at the time of object instantiation, dependencies are pushed into the object through the constructor arguments at the time of instantiating it. We can inject the dependency by constructor by using the <constructor-arg> sub element of <bean> in spring configuration file. <constructor-arg> element describes one argument of the constructor, which means to specify a constructor with multiple arguments we need to use <constructor-arg> element multiple times. The arguments specified by <constructor-arg> element will be associated automatically to the parameters of constructor. If there is an ambiguity due to number of parameters or due to data type of parameters then arguments must be matched by specifying type or index attribute in <constructor-arg> element.

Constructor Injection Example

Let's see the simple example to inject primitive and object-based values. To inject primitive values we should use value attribute and to inject object values we should use ref attribute of <constructor-arg>. While injecting object values ref attribute should be assigned with id value of other beans.

Project Directory Structure

constructor-injection-0
 

EmplyeeAdress.java

package com.java4coding;

 

public class EmplyeeAdress {

        public String city;

        public EmplyeeAdress(String city) {

                this.city = city;

        }

}

 

EmployeeBean.java

package com.java4coding;

 

public class EmployeeBean {

        public String name;

        public EmplyeeAdress add;

        public EmployeeBean(String name, EmplyeeAdress add) {

                super();

                this.name = name;

                this.add = add;

        }

}

 

EmployeeTest.java

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class EmployeeTest {

        public static void main(String[] args) {

                BeanFactory factory = new ClassPathXmlApplicationContext("springConfig.xml");

                EmployeeBean employeeBean = (EmployeeBean) factory.getBean("studentbean");

                System.out.println(employeeBean.add.city);

                System.out.println(employeeBean.name);

        }

}

springConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <constructor-arg name="city" value="Bangalore"></constructor-arg>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean">

                <constructor-arg name="add" ref="adrress"></constructor-arg>

                <constructor-arg name="name" value="Manu Manjunatha"></constructor-arg>

        </bean>

 

</beans>

Output

Bangalore

Manu Manjunatha

 

 


All Chapters
Author