Spring IoC Setter Value Injection

We will learn the process of setting property values of a bean using Spring IoC Setter Value Injection. For Spring IoC Setter Value Injection to work the bean class must contain setter of the property present.

In the following example we will consider a Person class which has two properties to construct the name of the person. We will set the property values in Spring IoC Bean Configuration file and the print the name of the person.

Person Bean class (Person.java)

package com.raistudies.beans;

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

    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;

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

Person class has two properties “firstName” and “lastName ” and their getter and setters.

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

In configuration file Person class has been configured as a Spring IoC Bean as you seen in the previous tutorial.

<?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">
        <property name="firstName" value="Carel"/>
        <property name="lastName" value="Staf"/>


<property/> tag is used to set the value or reference of a property in bean class. This tag has two attributes here. “name” attribute holds the name of the property in bean. In this case there are two properties in Person bean, “firstName” and “lastName”. The “value” attribute holds the value to be set on the property. Spring IoC container will call property setter methods to set the property value.

You can also do the same thing with following bean configuration:

<bean id="person" class="com.raistudies.beans.Person">
  <property name="firstName">
  <property name="lastName">

If you want to set null value to a property, then you can use two way to do it :

<bean id="person" class="com.raistudies.beans.Person">
  <property name="firstName">
  <property name="lastName">

Both the value will be set as null.

SpringBeanTestRunner class to test the example

Our SpringBeanTestRunner class contains the code to run the example. The runner class first create BeanFactory instance using our Spring IoC Bean Configuration file (app-config.xml) and then get the instance of person bean to call the method “getName’ in it.

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());

Output of the example code when run in Eclipse IDE :

Spring IoC Setter Value Injection Output

Spring IoC Setter Value Injection Output

You can download source or complete code from following links. Import the code in eclipse and run to test Spring IoC Setter Value Injection Output.

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>