Last updated on February 28th, 2024
Dependency Injection is the main feature provided by Spring container IoC(Inversion of Control). The Spring container dynamically assigns value to our resource(class/object) then it is called dependency injection.
Setter Injection:
• It is a type of Dependency Injection.
• Setter Injection(Spring Container calls setXxx(-) to inject dependent values).
In this project, we will create three classes. One class we will create for the Dependent class, the second class we will create for the Target class and the third class we will create for the Main class so that project run to read this class which contains the main method. We will create an XML configuration file for the Spring Bean class configuration.
In the one class, we will create a 0-parameter constructor which contains a simple print as a message to analyze when this 0-parameter constructor is executed, and also a method to print some message. Finally, we can confirm the class is instantiated properly and access its method.
In the second class, we will create a 0-parameter constructor which contains a simple print as a message to analyze first this 0-parameter constructor is executed, and create a setter method in which we pass the parameter type of the first class reference. So the Spring Container will use to inject the first class into the second class with this setter method.
In the XML configuration file, we can define these classes as Spring Bean and configure setter injection.
In the main class, we will activate Spring Container, load the XML configuration file and read this read file. We will retrieve the Spring bean class from the Spring Container. So we will access the Spring bean class’s method. Then we will get the output of the project on the console.
In this project, we analyze to configure Spring Bean setter injection in the XML configuration in a very easy manner.
In this example of the project, we are injecting a dependent class into the target class through Setter Injection.The CricketCoach POJO class is a dependent class and CricketTeam POJO class is a target class. These first two will configure as Spring Bean in the XML configuration file. So the Spring IoC container injects the CricketCoach Spring bean class into the CricketTeam Spring bean class through the setXxx(-) method of the CricketTeam spring bean class.
Development Process:
1. Keep eclipse IDE ready
2. Project Structure
3. Create a Dependent Bean Class
4. Create a Target Bean Class
5. Configure your Spring Beans
6. Add the jar file to the Build Path of the Project for this example of the setter injection with XML configuration
7. Run the App
1. Keep eclipse IDE ready
2. Project Structure
3. Create a Dependent Bean Class
CricketCoach.java(Pojo Class):
package com.poc.bean;
public class CricketCoach {
public CricketCoach() {
System.out.println("Constructor: 0-param constructor call of CricketCoach");
}
public String getDailyWorkout() {
return "The Team needs 4 hours for the bowling practice";
}
}
4. Create a Target Bean Class
CricketTeam.java(Pojo class):
package com.poc.bean;
public class CricketTeam {
private String name;
// dependent class
private CricketCoach teamCoach;
public CricketTeam() {
System.out.println("Constructor: 0-param is called(CricketTeam)");
}
// setter method
public void setName(String name) {
System.out.println("Team Name: inside setter method-setName");
this.name = name;
}
public String getName() {
return name;
}
// setter method
public void setTeamCoach(CricketCoach teamCoach) {
System.out.println("The Coach: inside setter method-setTeamCoach");
this.teamCoach = teamCoach;
}
public CricketCoach getTeamCoach() {
return teamCoach;
}
public String getInstruction() {
return "Team Name: " + name + "----" + teamCoach.getDailyWorkout();
}
}
→ This “setTeamCoach(-)” method Spring IoC calls to inject a CricketCoach object into the CricketTeam class.
5. Configure your Spring Beans
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Define your beans here-->
<!-- Define dependent bean -->
<bean id="myCoach" class="com.poc.bean.CricketCoach" />
<bean id="team" class="com.poc.bean.CricketTeam">
<!-- set up setter injection -->
<property name="name" value="Mumbai Indians "/>
<property name="teamCoach" ref="myCoach"/>
</bean>
</beans>
→ <property>tag configure bean property for setter injection.
→ In the above configuration, the spring container creates “CricketCoach”, and “CricketTeam” class objects using a 0-param constructor and calls setName(-) and setTeamCoach(-) methods to perform setter injection.
6. Add the jar file to the Build Path of the Project for this example of the setter injection with XML configuration
Right Click on Project(Setter_Injection_With_XML) ⇒ Build Path ⇒ Java Build Path ⇒ libraries ⇒ Add External jars
Commons-logging-<version>.jar
spring-beans-<version>.jar
spring-context-<version>.jar
spring-context-support-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
ClientApp.java:
package com.poc.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.poc.bean.CricketTeam;
public class ClientApp {
public static void main(String[] args) {
// load the spring Configuration file
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//retrieve bean from spring container
CricketTeam theTeam = context.getBean("team", CricketTeam.class);
// call the method on bean
System.out.println(theTeam.getInstruction());
context.close();
}
}
7. Run the App
Output:
Constructor: 0-param constructor call of CricketCoach
Constructor: 0-param is called(CricketTeam)
Team Name: inside setter method-setName
The Coach: inside setter method-setTheCoach
Team Name: Mumbai Indians ----The Team needs 4 hours for the bowling practice
Conclusion:
This example is explained What is Setter Injection? How to Configure the beans class in the XML file? How to create objects of Dependent and Target Beans? How to configure setter injection in the XML config file?