Setter Injection — Spring

Sagar Kudu
3 min readMar 30, 2021

Please read this article before, if you don’t know How to create a Configuration file and a Maven Project.

The Setter injection

setter method is to set the values.

Setter injection is also known as property injection as we use the <property> tag.

→ we need to use <property> tag for setter injection

→ if you have set fields using <property name=”YourFieldName”>…, then it is setter injection.

→ when an IoC container is creating an object, say Address object, it will call the Setter methods to put the values, and as soon as the setter method is called the setter method will have values ready i.e setCity(city) will set the value into the city.

→ e.g check the name of the field and also we are required to set its value for the given.

say we have, private String studentId;

Then,

<property name=”studentId”>

<value>1</value>

</property>

Similarly for private String studentName;

<property name=”studentName”>

<value>Sagar</value>

</property>

Steps to create beans → first create a project and add package name like ‘com.springcore

Step 1: Creating Student class

package com.springcore;

public class Student {

private int studentId;

private String studentName;

private String studentAddress;

//creating constructors.

public Student() {

super();

// TODO Auto-generated constructor stub

}

public Student(int studentId, String studentName, String studentAddress) {

super();

this.studentId = studentId;

this.studentName = studentName;

this.studentAddress = studentAddress;

}

//creating getter and setter.

public int getStudentId() {

return studentId;

}

public void setStudentId(int studentId) {

this.studentId = studentId;

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

this.studentName = studentName;

}

public String getStudentAddress() {

return studentAddress;

}

public void setStudentAddress(String studentAddress) {

this.studentAddress = studentAddress;

}

//generate toString()

@Override

public String toString() {

return “Student [studentId=” + studentId + “, studentName=” + studentName + “, studentAddress=” + studentAddress

+ “]”;

}

}

Step 2. Create a main method — App.java

Note: Now we have specified the metadata to Spring Container, Now using this metadata the spring container which dependencies container want, what are their values and all these we have specified into XML file.

Now whenever we will get the object, this is an advanced and ready-to-use object which is created by the Spring internally and hence it will provide the object.

Go in the Main java file and create an object but this time using will ask Spring to do this, How?

we will tell IoC container, hey please give us the object of ‘student1’ (specified in config.xml). This is called dependency injection.

→ Now we have to Instantiate a Container.

So we will create object Class of ClassPathXmlApplicationContext() using ApplicationContext Interface.

e.g new ClassPathXmlApplicationContext(); This will give reference of ApplicationContext context and using context reference we can get the beans using getBeans() method.

Note: context is our IoC Container.

package com.springcore;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App

{

public static void main( String[] args )

{

System.out.println( “Learning Setter Injection!” );

ApplicationContext context = new ClassPathXmlApplicationContext(“config.xml”);

// we know student1 is object of Student and we will typecast it

Student student1 = (Student) context.getBean(“student1”);

//checking that object are created or not

System.out.println(student1);

}

}

Step 3: Create a configuration file (config.xml) in src/main/java

<?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"

xmlns:context=”http://www.springframework.org/schema/context"

xmlns:p=”http://www.springframework.org/schema/p"

xmlns:c=”http://www.springframework.org/schema/c">

<bean class=”com.springcore.Student” name=”student1">

<property name=”studentId”>

<value>1</value>

</property>

<property name=”studentName”>

<value>Sagar</value>

</property>

<property name=”studentAddress”>

<value>Palghar</value>

</property>

</bean>

</beans>

Step 4: Running a project

Please run the project as Java Application and check the desired output.

Next topic: Constructor Injection

For queries reach out to me at sagar978@gmail.com.

--

--

Sagar Kudu

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Connect https://www.linkedin.com/in/sagarkudu/