spring setter injection example with xml configuration




HELLO READERS, In this spring tutorial we are going to learn about spring setter injection. 

In this example we are going to learn how to set value to a bean property through setter injection. 

Consider the below example Players  bean class which contains fullName, role and board attributes.

package com.TheTechMatin.Entity;

public class Players {

     private String fullName;
     private String role;
     private String board;

     public String getFullName() {
           return fullName;

     public void setFullName(String fullName) {
           this.fullName = fullName;

     public String getRole() {
           return role;

     public void setRole(String role) {
           this.role = role;

     public String getBoard() {
           return board;

     public void setBoard(String board) {
           this.board = board;

     public String toString() {
           return "Players [fullName=" + fullName + ", role=" + role + ", board=" + board + "]";


The Players class has three attributes i.e., name, role and board. For all the attributes setter & getter method is generated via ECLIPSE 2019-06. The toString() method is used to represent textual form the Players object.

The applicationContext.xml fie is used to do the spring bean configuration. The below xml file shows how to set/assign a value to bean property through spring setter injection.

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

     <bean id="player" class="com.TheTechMatin.Entity.Players">
           <property name="fullName" value="Sir Ravindra Jadeja"></property>
           <property name="role" value="All Rounder"></property>
           <property name="board" value="BCCI"></property>

The id attribute of the bean element is used to specify the bean name and class attribute is used to specify the fully qualified class name of the bean.

The property is the sub-element of bean element which is used to inject property value via setter injection, the name attribute of the property element represents the bean(class) attribute(for example String fullName and so on.) and value attribute is used to specify the corresponding property value.

Here in this example we set “Sir Ravindra Jadeja” for fullName attribute of Players class similarly “All Rounder” for role attribute and  BCCI” for board attribute.

Now executing the TestApp.java to  test the application.

package com.TheTechMatin.Entity;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

     public static void main(String[] args) {

//load configuration file
           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//jus printing msg
           System.out.println("Hello Welcome To Sping!");

//get the bean from applicationContext.xml
           Players player = context.getBean(Players.class);

//print the Players attributes value

//close the context

So when we execute the main class the following output will be displayed on the console.

Hello Welcome To Sping!
Players [fullName=Sir Ravindra Jadeja, role=All Rounder, board=BCCI]
Tested On Eclipse IDE 2019-06.

No comments

You May Like Also

Programming Knowledge

Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database