Spring Inversion Of Control(IOC) vs Dependency Injection(DI)

Inversion Of Control (IOC)

This is common characteristic of frameworks, IOC manages java objects from instantiation to destruction through its BeanFactory.
Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean’s scope, lifecycle events, and any AOP features for which it has been configured and coded.

 

Dependency Injection(DI)

The basic concept of the dependency injection (also known as Inversion of Control pattern) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. An IOC container is then responsible for hooking it all up.

For example :

Suppose we have an object Employee and it has a dependency on object Address. So we define a bean corresponding to Employee where it will define its dependency on object Address. When Spring tries to create an Object Employee it sees that Employee has a dependency on object Address so first it will create the Address object (dependent object) and then inject this into the Employee Object.

Normal way (Without dependency Injection)

Here Employee class has dependency on address object. So we have instantiated address object in traditional way using new operator as below –

 

 

 

 

 

 

 

With Dependency Injection –

Here we have outsourced the object creation task to third party in our case it’s spring container. In spring these objects are called as beans. These beans can be configured using XML or Annotation based configuration as shown in earlier posts. Employee objects has a dependency on Address . So this dependency is injected by third party . This process is called  Dependency Injection (DI).

 

 

 

 

 

Benifits of Dependency Injection in Spring:

  • Ensures configuration and uses of services are separate.
  • Can switch implementations by just changing configuration.
  • Enhances Testability as mock dependencies can be injected.
  • Dependencies can be easily identified.

How to Implement Inversion of Control

In object-oriented programming, there are several basic techniques to implement inversion of control as below-

  1. Using a factory pattern
  2. Using a service locator pattern
  3. Using a dependency injection of any given below type:
    • a constructor injection
    • a setter injection
    • an interface injection

Constructor Injection

Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator. Additionally, calling a static factory method with specific arguments to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to a static factory method similarly.

public class ConstructorDI {

DemoBean demoBean = null;

public TestSetterDI (DemoBean demoBean) {
this.demoBean = demoBean;
}
}

Setter Injection

Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

public class SetterDI {

DemoBean demoBean = null;

public void setDemoBean(DemoBean demoBean) {
this.demoBean = demoBean;
}
}

Interface Injection

In this methodology we implement an interface from the IOC framework. IOC framework will use the interface method to inject the object in the main class. It is much more appropriate to use this approach when you need to have some logic that is not applicable to place in a property. Such as logging support.

public void SetLogger(ILogger logger)
{
_notificationService.SetLogger(logger);
_productService.SetLogger(logger);
}

Leave a Comment

Bitnami