spring dependency injection and inversion of control

Dependency Injection or Inversion of Control

Before we begin to understand the concept of DI and IoC. It is very important to understand the concept loose coupling and tight coupling.

Term (Tight Coupling)

When two or more classes are dependent to each other is said to be tightly coupled classes. When we change one object which is tightly coupled with the application, it requires changes to all dependent classes. We can handle it in small level application but in case of enterprise level application. It will be very difficult to manage dependencies.

For example

public class A {
 private MessageHandler messageHandler;
 
 public A() {
 messageHandler = new MessageHandler();
 }
 
 public void someActions() {
 
 messageHandler.displayMessage(message);
 }
}

public class MessageHandler {
 
 public void displayMessage(String message) {
 // display message
 }
}
In the above example, MessageHandler classes is tightly coupled with the class A to perform display message action.  

Term (Loose Coupling)

Loose coupling is the process in which the goal is to reduce the interdependencies between components of a system. Which reduces the risk that changes in one dependency will require the changes in all dependent components of a system. Loose coupling design provides flexibility of a system, maintainable architecture.

For example
public class A {
 private MessageHandler messageHandler;
 
 public void setMessageHandler(MessageHandler messageHandler) {
 this.messageHandler = messageHandler;
 }
 
 public void someActions() { 
 messageHandler.displayMessage(message);
 }
}

public interface MessageHandler {
 public void displayMessage(String message);
}

public class MessageHandlerImpl implements MessageHandler {
 
 public void displayMessage(String message) {
 // display message
 }
}
In the above example, we reduced the dependency from class A in order to display message. Now we can inject MessageHandler class to any class which needs to display message.

Also in near future any other class can implement MessageHandler to provide their own definition to display message and can be injected without any modification to the class. Now we better understand the concept of loose coupling and tight coupling, so we are ready to understand the concept of Dependency Injection and Inversion of Control.  

DI or IoC

Spring’s most powerful and useful features are Dependency Injection or Inversion of Control. Dependency Injection and Inversion of Control are mainly used to achieve loose coupling in application.

Dependency Injection is the process where objects define their dependencies with each other, that is, with the help of Constructor Injection, property Injection or arguments to factory methods. After the dependent objects are constructed or returned by the Factory methods.

Container then injects those dependencies to the actual bean and create that bean. And the whole process is inversion of control. Now spring container is taking the responsibility of object’s life cycle and their dependencies. We don’t need to create any hard coded dependency inside our business logic. We can instruct the container how two or more classes are dependent on each other.

Rest of the thing container will handle. There are mainly two packages which are the base of Spring framework’s IoC container (org.springframework.beans and org.springframework.context). We will discuss them later.

BeanFactory interface is responsible to manage the life cycle of the bean. It provides configuration mechanism capable of managing any type of objects.

On the other hand, ApplicatioContext is the advance version of BeanFactory provides more specific functionality.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *