Springs - SPLessons

Chapter 8

Spring Dependency Injection

Spring Dependency Injection

Spring Dependency Injection is the main heart of the Spring framework, the functionality of Spring Dependency Injection is to remove the dependencies in the code, the advantage of dependency injection is to make the code loosely coupled, it overcomes the drawbacks of dependency look up, dependency lookup is the way where developer get the resource after the demand. Dependency look up makes the code tightly coupled means that once resource is changed, developer has to do more changes in the code, testing of an application is also not possible, but these problems can be removed by dependency injection. In Spring framework dependency injection will be performed in two ways as follows.

Constructor Based Dependency Injection

While using constructor injection, <constructor-arg> is the element utilized in bean.xml file. Following is an example which explains more about this concept.

Following is the content of another dependent class file SpellChecker.java .
SpellChecker.java

package splessons;

public class SpellChecker {
	   public SpellChecker(){
	      System.out.println("Inside SpellChecker constructor." );
	   }

	   public void checkSpelling() {
	      System.out.println("Inside checkSpelling." );
	   }
	   
	}

Here is the content of TextEditor.java file.
TextEditor.java

package splessons;

public class TextEditor {
	   private SpellChecker spellChecker;

	   public TextEditor(SpellChecker spellChecker) {
	      System.out.println("Inside TextEditor constructor." );
	      this.spellChecker = spellChecker;
	   }
	   public void spellCheck() {
	      spellChecker.checkSpelling();
	   }
	}

Following is the content of the MainApp.java file.
MainApp.java

package splessons;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      TextEditor te = (TextEditor) context.getBean("textEditor");

      te.spellCheck();
   }
}

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface. Following is the configuration file Beans.xml which has configuration for the constructor-based injection.
Beans.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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="splessons.TextEditor">
      <constructor-arg ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="splessons.SpellChecker">
   </bean>

</beans>

Output
When compile the code following is the result will be displayed.

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

Setter Based Dependency Injection

While using constructor injection, <property> is the element utilized in bean.xml file. Following is an example with same concept as above which explains more about this concept.

Here is the content of TextEditor.java file.
TextEditor.java

package splessons;

public class TextEditor {
	   private SpellChecker spellChecker;

	   // a setter method to inject the dependency.
	   public void setSpellChecker(SpellChecker spellChecker) {
	      System.out.println("Inside setSpellChecker." );
	      this.spellChecker = spellChecker;
	   }
	   // a getter method to return spellChecker
	   public SpellChecker getSpellChecker() {
	      return spellChecker;
	   }

	   public void spellCheck() {
	      spellChecker.checkSpelling();
	   }
	}

To set a variable spellChecker we are using setSpellChecker() method which is very similar to Java POJO classes.
SpellChecker.java

package splessons;

public class SpellChecker {
	   public SpellChecker(){
	      System.out.println("Inside SpellChecker constructor." );
	   }

	   public void checkSpelling() {
	      System.out.println("Inside checkSpelling." );
	   }
	   
	}

Following is the content of the MainApp.java file .
MainApp.java

package splessons;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      TextEditor te = (TextEditor) context.getBean("textEditor");

      te.spellCheck();
   }
}

Following is the configuration file Beans.xml which has configuration for the setter-based injection.
Beans.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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="splessons.TextEditor">
      <property name="spellChecker" ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="splessons.SpellChecker">
   </bean>

</beans>

Output
When compile the code following is the result will be displayed.

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

Summary

  • Spring Dependency Injection is a design pattern.
  • Spring Dependency Injection with setter method will utilize property tag.