Spring IoC Constructor Type Injection

In this tutorial, we will learn how we can provide constructor argument value of a bean using data type of argument. Spring IoC Constructor Type Injection is a method in which we provide dependency data on the basis of data type of the Constructor Type. If more than one argument is present in the Constructor of same data type than Spring IoC Container set the value according to the position of the arguments respectively.

Our following example will show the process of using Spring IoC Constructor Type Injection in practical. In our example, there is Peron class which we will configure as a Spring Bean.

Person Bean Code (Person.java)

package com.raistudies.beans;

public class Person {
    private String firstName = null;
    private String lastName = null;
    private int age;

    public Person(String firstName, String lastName, int age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;

    public String getPersonDetail(){
        return "Name :" + firstName + " " + lastName +"\nAge : " + age;

Person bean contains three properties,”firstName”, “lastName” and “age”, which are set through constructor of Person class.

Spring IoC Bean Configuration file(app-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="person" class="com.raistudies.beans.Person">
        <constructor-arg type="int" value="21"/>
        <constructor-arg type="java.lang.String" value="Rita"/>
        <constructor-arg type="java.lang.String" value="Josap"/>


In our Spring IoC bean configuration file, Person class has been configured as a Spring bean. To set the properties of Person bean we have used Spring IoC Constructor Type Injection.

<constructor-arg type=”int” value=”21″/>: <constructor-arg/> tag specifies that the property should be set using class constructor. There are two attributes we have used in this configuration. “type” attribute says the data type of the property and “value” attribute indicates the value of the property. If there are more then one constructor argument with same data type, spring will set the value sequentially.

SpringBeanTestRunner class to test the example

package com.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.raistudies.beans.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        System.out.println("Initialing bean factory");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("app-config.xml");
        System.out.println("Getting person bean");
        Person person = (Person)beanFactory.getBean("person");
        System.out.println("Person Detail is " + person.getPersonDetail());

SpringBeanTestRunner class helps us to test our example. It first creates BeanFactory instance from ClassPathXmlApplicationContext class which loads our Spring IoC Bean Configuration file and configues Person class as a bean. Then, “getBean” method of BeanFactory is used to get the instance of Person class. It prints details of person bean get from Spring IoC container.

Running the example in Eclipse IDE, we get the following output:

Spring IoC Constructor Type Injection Output

Spring IoC Constructor Type Injection Output

You can download example source or full project using following links. Import the code in Eclipse and run to test the functionality of Spring IoC Constructor Type Injection.

Source: Download

Source + lib: Download

Related Posts:

Leave a comment ?


  1. Way awesome, some legitimate factors! I value you producing this informative article accessible, the remainder of the internet site can also be substantial high quality. Have a very enjoyable.

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>