Spring IoC Constructor Value Injection

Spring IoC Constructor Value Injection is a method of constructor injection in which the value of the attributes of a bean are set using constructor of the class. That means the constructor of the bean must be defined to accept attributes values and set the values to bean properties.

We will see the method of using Spring IoC Constructor Value Injection to set dependencies in Person bean.

Person Bean Code (Person.java)

package com.raistudies.beans;

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

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

    public String getName(){
        return firstName + " " + lastName;

Here, Person bean has two properties and constructor of Person bean allow to set the value of the properties. So, we can use Spring IoC Constructor Value Injection to inject the value of “firstName” and “lastName” in Person.

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

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

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

    <bean id="person" class="com.raistudies.beans.Person">
        <constructor-arg name="firstName" value="Rita"/>
        <constructor-arg name="lastName" value="Josap"/>

In Spring IoC Bean Configuration file, we have configure Person class as a spring bean. <constructor-arg/> tag is used to set constructor parameter values in Spring IoC container. “name” attribute is used to specify the name of the constructor parameter and “value” attributes says the value to be set on it.

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 Name is " + person.getName());

In our example runner class, uses ClassPathXmlApplicationContext to load the Spring IoC configuration file “app-config.xml” and configure the beans specified in the configuration file. BeanFactory instance is used to get the person bean and “getName” method is used to print the name in Person bean instance.

On running the example in Eclipse IDE, it gives following output:

Spring IoC Constructor Value Injection Output

Spring IoC Constructor Value Injection Output

You can download source code or full project from following link. Import the code in Eclipse IDE and test the example to more clearly understand Spring IoC Constructor Value Injection.

Source: Download

Source + lib: Download

Related Posts:

Leave a comment ?


  1. Here name is not allowing. is it possible to specify name attribute for constructor injection

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>