Dependency Injection(DI) or Inversion of Control(IoC) is the prominent feature of Spring Framework. Dependency Injection is a design pattern which removes the dependency between objects so that our code becomes loosely coupled. In Spring there exist two major types of Dependency Injection.
Setter Injection :
Setter Injection is the simplest Dependency Injection which injects the values via the setter method
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 injects the value to its property through the Constructor avaiable
Student.java
We have removed the setter method in our Student class and have added 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