Meyyappan has posted 101 posts at DZone. View Full User Profile

Spring Setter Injection

06.14.2012
| 26177 views |
  • submit to reddit

In this example you will learn how to set a bean property via setter injection. Consider the following User bean class.

package com.vaannila;

public class User {

    private String name;
    private int age;
    private String country;
    
    public String getName() {
    	return name;
    }
    
    public void setName(String name) {
    	this.name = name;
    }
    
    public int getAge() {
    	return age;
    }
    
    public void setAge(int age) {
    	this.age = age;
    }
    
    public String getCountry() {
    	return country;
    }
    
    public void setCountry(String country) {
    	this.country = country;
    }
    
    public String toString() {
    	return name + " is " + age + " years old, living in " + country;
    }
}

 The User bean class has three attributes viz. name, age and country. All the three attributes are set using the setter injection. The toString() method of the User bean class is overridden to display the user object.

Here the beans.xml file is used to do spring bean configuration. The following code shows how to set a property value thru setter injection.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.vaannila.User" >
        <property name="name" value="Eswar" />
        <property name="age" value="24"/>
        <property name="country" value="India"/>
    </bean>

</beans>

The id attribute of the bean element is used to specify the bean name and the class attribute is used to specify the fully qualified class name of the bean. The property element with in the bean element is used to inject property value via setter injection. The name attribute of the property element represents the bean attribute and the value attribute specifies the corresponding property value.

Here we set "Eswar", "24" and "India" for the User bean properties name, age and country respectively. The following Main class is used to get the User bean from the Spring IoC container and dispaly its value it to the user.

package com.vaannila;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support. ClassPathXmlApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User)context.getBean("user");
        System.out.println(user);
    }

}

 On executing the Main class the following message gets displayed on the console.

Eswar is 24 years old, living in India

You can download and try the Spring setter injection example by clicking the Download link below. 

Source:  Download

Source + Lib:  Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: