Spring IoC Constructor Index Injection

We will learn in this tutorial to set the value of dependencies of spring bean using constructor of the bean by defining the position or sequence number of the argument in constructor. Spring IoC Constructor Index Injection is the method to use index value of the constructor argument to resolve dependency. But, you have to always take care about the data type of the argument and value you are setting. Otherwise Spring IoC Container will through error.

Following example will illustrate you the method of using Spring IoC Constructor Index Injection with a Spring bean, Person. Person class has three properties “firstName”, “lastName” and “age”. We will set properties of Constructor Index Injection.

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 class constructor has three arguments to set the value to properties. First argument is “firstName” , so it’s index will be 0. Then, the second argument is “lastName”, hence its index will be 1 and similarly index of “age” field will be

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 index="2" value="21"/>
        <constructor-arg index="0" value="Rita"/>
        <constructor-arg index="1" value="Josap"/>


We have configure our Person class as a Spring IoC Bean in Spring IoC Bean Configuration file. You will get one new thing in this configuration:

<constructor-arg index=“2” value=“21”/>: <constructor-arg/> tag configure the constructor argument value to be set while Spring IoC Container will create the instance of the bean. “index” will specify the position of the argument in bean construction ans “value” will specify the value to be set. we can also use ref in place of value if we want to set a bean reference.

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 will run the example to test. It is using ClassPathXmlApplicationContext to load and configure our Spring IoC Bean configuration file which is assigned to instance of BeanFactory. BeanFanctory instance is used to get the instance of person bean and then it get the person detain from person instance and print it on command prompt.

Run the code on Eclipse, following output will come:

Spring IoC Constructor Index Injection Output

Spring IoC Constructor Index Injection Output

You can download the source code or full example project from following links:

Source: Download

Source + lib: Download

Related Posts:

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>