DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Spring Setter Injection

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · · Tutorial
Like (0)
Save
Tweet
53.49K Views

Join the DZone community and get the full member experience.

Join For Free

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

Spring Framework Injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes Service Types Explained In-Detail
  • How To Evaluate Software Quality Assurance Success: KPIs, SLAs, Release Cycles, and Costs
  • Reactive Kafka With Streaming in Spring Boot
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo