SPRING SETTER INJECTION EXAMPLE
WITH XML CONFIGURATION.
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.
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.
Players.java
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;
}
@Override
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.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<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>
</bean>
</beans>
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.
TestApp.java
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
System.out.println(player);
//close the context
context.close();
}
}
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