30/10/2028 09:08 Spring Sette Injection -javatpoint
nites javatpoint convsering tutorial-dependency-inection-by-setter-method 90/10/2028 09:08 ‘Spring Setter Injection -javatpoint
Dependency Injection by setter method
We can inject the dependency by setter method also. The
subelement of is
used for setter injection. Here we are going to inject,
1. primitive and String-based values
2, Dependent object (contained object)
3. Collection values etc.
Injecting primitive and string-based values by setter method
Let's see the simple example to inject primitive and string-based values by setter method. We have
created three files here’
© Employee java
© applicationContext.xm!
© Testjava
https: javatpoint.comi/spring-utoril-dependency-injection-by-selter- method
21930/10/2028 09:08 ‘Spring Setter Injection -javatpoint
Dx
Employee java
Itis a simple class containing three fields id, name and city with its setters and getters and a method
to display these informations
package com avatpoint;
pul
private int id;
ic class Employee {
private String name;
private String city;
public int getld( {
return id;
}
public void setid(int id) {
this.id = id;
}
pul
return name;
}
pul
this.name = name;
ic String getName() {
void setName(String name) {
public String getCity( {
return city;
}
public void setCity(String city) {
tips: javatpoint.comispring-utoril-dependency-injection-by-selter-method a9‘sortoz02e 09.04 ‘Sprang Sete Ijcton-javatpoint
this.city = city;
}
void display
System.out printin(id+" "+name+
city);
applicationContext.xml
We are providing the information into the bean by this file. The property element invokes the setter
method, The value subelement of property will assign the specified value,
tips: javatpoint.comispring-utoril-dependency-injection-by-selter-method 490/10/2028 09:08 Spring Sette Injection -javatpoint
20
Arun
ghaziabad
Test.java
This class gets the bean from the applicationContext.xm! file and calls the display method.
package comjavatpoint;
import org springframework.beans.factory.BeanFactory;
import org springframework.beans.factory.xml.XmlBeanFactory;
import org springframework.core.io.*;
public class Test (
public static void main(Stringl] args) {
Resource r=new ClassPathResource("applicationContext.xm!");
BeanFactory factory=new XmIBeanFactory(1);
Employee e=(Employee}factory.getBean("obj);
sdisplay0;
nites javatpoint convsering tutorial-dependency-inection-by-setter-method 5930/10/2028 09:08 ‘Spring Setter Injection -javatpoint
Output:20 Arun ghaziabad
download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE)
a)
‘outube For Videos Join Our Youtube Channel: Join Now
Feedback
© Send your Feedback to feedback @javatpoint.com
Help Others, Please Share
oa
Learn Latest Tutorials
(Splunk tutorial tutorial 2 Swagger
‘Splunk ‘SPSS tutorial
Swagger
(#Tumbrr tutorial React tutorial # Regex tutorial
tips javatpoint.comispring-utoril-dependency-injection-by-selter- method
5QL tutorial
Transact-SQL,
w# Reinforcement
learning tutorial
as30/10/2028 09:08
Tumblr
PR Programming
tutorial
R Programming
& Python Pillow
tutorial
Python Pillow
Preparation
Aptitude
Aptitude
2 Company
Interview
Questions
Company Questions
ReactIS.
(ARXIS tutorial
RIS.
& Python Turtle
tutorial
Python Turtle
2 Logical
Reasoning
Reasoning.
Trending Technologies
2 Artificial
Intelligence
Artificial
Intelligence
PHadoop tutorial
Hadoop
(ZAWS Tutorial
AWS
ReactIS
‘Tutorial
ReactIS
‘Spring Setter Injection -javatpoint
Regex
1) React Native
tutorial
React Native
#iKeras tutorial
Keras
Lpiverbal Ability
Verbal Ability
Selenium
tutorial
Selenium
1) Data Science
Tutorial
Data Science
tipsy javatpoint.comispring-utoril-dependency-injection-by-selter-method
Reinforcement
Learning,
i) Python Design
Patterns
Python Design
Patterns
2 Interview
Questions
Interview Questions
2 Cloud
‘Computing
Cloud Computing
2 Angular 7
Tutorial
Angular 7
11930/10/2028 09:08
# Blockchain
Tutorial
Blockchain
B.Tech / MCA
DBMS tutorial
DBMS
B Computer
‘Network tutorial
Computer Network
Ethical Hacking
Ethical Hacking
Cyber Security
tutorial
Cyber Security
Java tutorial
Java
2 Control
‘Systems tutorial
Control System
AGit Tutorial
Git
# Data Structures
tutorial
Data Structures
Compiler
Design tutorial
Compiler Design
Computer
Graphies Tutorial
Computer Graphics
Automata
Tutorial
Automata
we Net
Framework
tutorial
Net
i. Data Mining
‘Tutorial
Data Mining
‘Spring Setter Injection -javatpoint
2) Machine
Learning Tutorial
Machine Learning
DAA tutorial
DAA
Computer
‘Organization and
Architecture
Computer
Organization
2) Software
Engineering
Software
Engineering
A C Language
tutorial
C Programming
\e:Python tutorial
Python
2) Data
Warehouse
Tutorial
Data Warehouse
tipsy javatpoint.comispring-utoril-dependency-injection-by-selter-method
i) DevOps
Tutorial
DevOps
2 Operating
System
Operating System
#2. Discrete
Mathematics
Tutorial
Discrete
Mathematics
html tutorial
Web Technology
ACH tutorial
cH
2 Listof
Programs
Programs
as30/10/2028 09:08 Spring Sette Injection -javatpoint
nites javatpoint convsering tutorial-dependency-inection-by-setter-method a9