Thinking how you could use dependency injection in Java Spring Framework?
There are commonly three types of dependency injection in Spring Framework
- Reference Injection
- Setter Injection
- Constructor Injection
We are going to discuss about the "Reference Injection" , why? Because it is the one which is mostly used in real world applications.
Step 1 Add the following dependencies to maven project
[Spring Core]
(mvnrepository.com/artifact/org.springframew..)
[Spring-Context]
(mvnrepository.com/artifact/org.springframew..)
Step 2 Create a reference package or you could name it anything (I've named it reference) after that create these two classes in that package
Programmer.java
Google.java
Why are we creating such class files? Well to make it very clear how the dependency injection works we are taking a real-world example.
A programmer is always dependent upon Google , not always but 95% of the time
Step 3
Put the following code in your Programmer.java
class
package com.core.reference;
public class Programmer {
private String name;
private Google google;
//Default Constructor
public Programmer(){super();}
public Programmer(String name, Google google) {
this.name = name;
this.google = google;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Google getGoogle() {
return google;
}
public void setGoogle(Google google) {
this.google = google;
}
@Override
public String toString() {
return "Programmer{" +
"name='" + name + '\'' +
", google=" + google +
'}';
}
}
Step 4
Put the following code in your Google.java
class
package com.core.reference;
public class Google {
private String searchQuery;
//Default Constructor
public Google(){
super();
}
public Google(String searchQuery) {
this.searchQuery = searchQuery;
}
public String getSearchQuery() {
return searchQuery;
}
public void setSearchQuery(String searchQuery) {
this.searchQuery = searchQuery;
}
@Override
public String toString() {
return "Google{" +
"searchQuery='" + searchQuery + '\'' +
'}';
}
}
WHOOPS.....
We forgot to define the beans for those class files
Create a new directory (if not exists) named resources
Let's create a
programmer.xmlfile in the resources directory
Step 5 Define your beans in the XML file
<?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">
<!--
Defining the beans
-->
<!--
For Google Bean
-->
<bean name="googleRef" class="com.core.reference.Google">
<property name="searchQuery" value="How to blah blah ..." />
</bean>
<!--
For Programmer Bean
-->
<bean name="programmer" class="com.core.reference.Programmer">
<property name="name" value="Unknown"/>
<property name="google">
<ref bean="googleRef"/>
</property>
</bean>
</beans>
YAAYYY ! We're almost done!
Final Step
Create the Test.java
class and put the following code in in that file.
package com.core.reference;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("programmer.xml");
Programmer programmer=(Programmer) context.getBean("programmer");
System.out.println(programmer);
}
}
Conclusion
The programmer is dependent on Google he needs google to search for his query. So, we defined two beans
- One for Programmer class
- Second for Google class
In the XML file you can clearly see. We injected the bean named "googleRef" into the programmer bean through the "ref" tag and given the reference of "googleRef" so the programmer can search for his query
:))
I hope this helped you
Share with your friends to grow their knowledge
Thank you!