Home » Spring » Spring Core Module, Introduction to Inversion of Control

Spring Core Module, Introduction to Inversion of Control

Spring Core Module is the most important Module of Spring.Inversion Of Control(IOC) or Dependency Injection is one of best way to implement loosely coupled application.

Using DI.,the framework acts as an Object Factory to build service objects and inject those service objects to application based on some runtime configuration file.

IOC makes the code simpler and easy to test.The IOC container manages the pojo and its dependencies in the application. In IOC the application defines the dependency and these dependencies are then satisfied at the Runtime by the IOC container as given below.

  • A caller asks the container for an object with a specific name or a specific type.
  • The container injects these objects by name to other objects, via either constructors,properties or factory methods.
  • Objects are not required to be created. Dependencies are injected into the objects that need them.
  • Classes are not required to be coupled in any specific implementation which is the biggest advantage of loose coupling.Since an object knows its dependency by their interface, the dependency can be swapped out with a different implementation.

In Spring there are many ways to wire components but a common approach is via XML.Typically the container is configured by loading XML files with some runtime configuration containing bean definition which provide the information required to create the beans.

Before getting into this tutorial we need to understand what is tight coupling and loose coupling in java. So let us try to understand about tight and loose coupling between java objects.

A Tightly Coupled Application

Consider the following application.

Bank.java

package com.jwt.spring;
public interface Bank {
	public int getRateOfInterest();
}

The Bank interface has a method to calculate Rate Of Interest.


CitiBank.java

package com.jwt.spring;

public class CitiBank implements Bank {

	@Override
	public int getRateOfInterest() {
		return 15;
	}
}

HdfcBank.java

package com.jwt.spring;

public class HdfcBank implements Bank {

	@Override
	public int getRateOfInterest() {
		return 13;
	}
}

The CitiBank and HdfcBank classes implements the Bank interface and the classes generate interest rate related to Citi Bank and HdfcBank.


BankService.java

package com.jwt.spring;

public class BankService {
	Bank bank = new HdfcBank();

	public void rateOfInterest() {
		System.out.println("Rate of Interest is " + bank.getRateOfInterest()+ "%");
	}
}

The BankService class displays the rate of interest and it holds the reference of Bank interface.


BankApplication.java

package com.jwt.spring;

public class BankApplication {
	public static void main(String as[]) {
		BankService bankService = new BankService();
		bankService.rateOfInterest();
	}
}

BankApplication is client class which is calculating Interest Rate.


Output :

Rate of Interest is 13%

The components of this application is tightly coupled. An instance of Bank in the BankService class is created as :

For CitiBank

Bank bank = new CitiBank();

For HdfcBank

Bank bank = new HdfcBank();

A Loosely Coupled Application

To avoid tight coupling, use dependency injection where the component only needs a way to accept the resources and the container will deliver the resources to the components.

Following is updated version of BankService class. It demonstrates a loosely coupled application.

BankService.java

package com.jwt.spring;

public class BankService {
	Bank bank;

	public void setBank(Bank bank) {
		this.bank = bank;
	}

	public void rateOfInterest() {
		System.out.println("Rate of Interest is " + bank.getRateOfInterest()
				+ "%");
	}
}

BankService now doesn't directly create instance of Bank interface, instead of that it will now make use of container to do this. The container will inject the required dependencies.

Dependencies are injected using the setter or Constructor injection. BankService uses the Setter Injection. The value for the Bank is set using setBank() method.


Previous Next Article
comments powered by Disqus