Dependency Injection(DI) is the most important feature of Spring Framework. Dependency Injection is a design pattern that removes the dependency between objects so that the objects are loosely coupled. In Spring there exist two major types of Dependency Injection.
Before getting into the code let’s get some basic understanding of what is Inversion of Control ? and What is Dependency Injection?
Spring Dependency Injection
What is Inversion of Control?
Inversion of Control is a Software Design Principle, independent of language, which does not actually create the objects but describes the way in which object is being created. Inversion of Control or IoC is the principle, where the control flow of a program is inverted: instead of the programmer controlling the flow of a program, the framework or service takes control of the program flow.
In other words, we can say IoC is a generic terminology where instead of the application calling the methods in the framework, the framework calls implementations provided by the application.
What is Dependency Injection?
Dependency Injection is the pattern through which Inversion of Control achieved, Through Dependency Injection, the responsibility of creating objects is shifted from the application to the Spring IoC container. It reduces coupling between multiple objects as it is dynamically injected by the framework.
Through Dependency Injection, we will be providing the dependencies of the object at run time by using different injection techniques such as Setter Injection or Constructor Injection.
Setter Injection :
Setter Injection is the simplest Dependency Injection which injects the values via the setter method. Spring container uses setXXX() of the Spring bean class to assign a dependent variable to the bean property from the bean configuration file. Setter injection is the most preferable injection, when there is a large number of dependencies need to be injected.
Student.java
It is a simple java class containing the getters and setters of name property.
package com.javainterviewpoint; public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
SpringConfig.xml
The SpringConfig.xml has the bean definitions
- We have set bean id as “student” for our Student class which will act as the reference for calling our Student class at later point.
- Using <property> tag we have set the values to the property(name) of the Student class(Setter Injection)
<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-3.0.xsd"> <bean id="student" class="com.javainterviewpoint.Student"> <property name="name" value="JavaInterviewPoint"></property> </bean> </beans>
ClientLogic.java
- Read the Configuration file(SpringConfig.xml) and get all the bean definition through BeanFactory
- Get the Student Class instance by calling the getBean() method over the bean factory.
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
package com.javainterviewpoint; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the Student instance Student student = (Student)bf.getBean("student"); System.out.println("Setter Injection value set : "+student.getName()); } }
Output
Setter Injection value set : JavaInterviewPoint
Constructor Injection :
Constructor Injection is the process of injecting the dependencies of an object through its constructor argument at the time of instantiating it. Or simply we can the dependencies of the object is supplied through the objects own constructor. Ioc Container can use zero or more arguments to initiate the bean. We will be using <constructor-arg> tag which is a subelement of the <bean> tag for Constructor Injection.
Student.java
We have removed the setter method in our Student class and have added a constructor which sets the value to the name property.
package com.javainterviewpoint; public class Student { private String name; public Student(String name) { this.name=name; } public String getName() { return name; } }
SpringConfig.xml
The SpringConfig.xml which has the bean definitions also changed a bit. we have added <constructor-arg> tag instead of the <property> tag. The <constructor-arg> tag injects the value to the name property.
<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-3.0.xsd"> <bean id="student" class="com.javainterviewpoint.Student"> <constructor-arg value="JavaInterviewPoint"></constructor-arg> </bean> </beans>
ClientLogic.java
There will be no change in the ClientLogic class.
package com.javainterviewpoint; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the Student instance Student student = (Student)bf.getBean("student"); System.out.println("Setter Injection value set : "+student.getName()); } }
Output
Constructor Injection value set : JavaInterviewPoint
We will learn more about Setter and Constructor Injection in our forth coming articles.
Leave a Reply