Tag Archives: IoC

What is Spring IoC ?

Before going through the Spring IoC tutorial, we must know what IoC is and why we use it in real life project development.

IoC or Inversion of Control, is also known as Dependency Injection, is used to create loosely coupled software components. Here, loosely coupled means less dependent software components. Let us illustrate it with an example:

There is a Report class used to generate reports of a bank account and ReportFileWriter is a class that creates a file and writes the report data to the file. Data is provided by the Report class to ReportFileWriter and in response it returns the file instance containing the report. So Report class is dependent on ReportFileWriter class. The dependency graph will be as follow:

Dependency Graph

In this situation Report class will create an instance of class ReportFileWriter and will call the method “createReport” by passing report data:

ReportFileWriter fw = new ReportFileWriter();
fw.createReport(data);

If you think carefully, in this code we are using tight coupled dependency between Report class and ReportFileWriter class. That means if we want to change the file writer class to PDFReportFileWriter in future we have to change the code of Report class. They are tightly coupled.

IoC solves the problem of tight couple and helps to makes the software component loosely coupled, so that we can maintain our code pretty much easily.

IoC or Inversion of Control is a design pattern that provided instances of a bean or class to another when they needed.That means, we do not have to use new keyword to create instance of dependent class. We will just create a reference of dependent class and IoC pattern will set the instance to the reference when needed.

Now, using IoC Report class will have following code :

ReportFileWriter fw = null;

Public void setFw(ReportFileWriter fw){
    this.fw = fw;
}

IoC container will call setter method to set the instance of ReportFileWriter class. We have to configure the dependency in our classes in IoC configuration file. That will tell the IoC container when and how to inject the dependences.

Creating a better loosely coupled model

In this session we will see how to IoC to create effective loosely coupled components. Consider the previous example of Report class. There is a interface named “ReportWriter” which contains following definition:

public interface ReportWriter {
    public File createReport(ReportData data);
}

Any class implementing ReportWriter interface will be able to create report file of its own format. The report class contains following code:

private ReportWriter reportWriter = null;

public void setReportWriter(ReportWriter rw){
    this.reportWriter = rw;
}

//More Code
reportWriter.createReport(data);
//More Code

Now, we want to provide a facility to generate report in two formats, in PDF and in MS Word. So we will create two classes PDFReportWrite and MSWordReportWriter which will implement the interface ReportWriter. We will configure our IoC container in such a way that when we will need a PDF Report the IoC container will create instance of PDFReportWrite and set to ReportWriter reference of Report class and if we need MS Word format then IoC container will create instance of MSWordReportWriter and set to ReportWriter reference of Report class. No more we have to change the code of Report class to generate a different format report. This is the advantage of IoC design pattern.

Types of Dependency Injection:

There are three ways in which a IoC container can inject dependency class instance to the dependent class:

Setter Injection: In this way, IoC container uses setter of the reference to set the instance of dependency class. Spring IoC provides setter Injection facility. We will learn about it with proper examples in next tutorial.

Constructor Injection: In constructor injection IoC container users construction of dependent class to set the instance of dependency class. We will see example of constructor injection in upcoming tutorials using Spring IoC framework.

Interface Injection:It is more complex process of dependency injection in which interfaces are used to inject the instances. Spring IoC container does not support this type of injection.