Dependency Injection (DI) and Inversion of control (IoC)

Dependency Injection (DI) and Inversion of control (IoC)

What is Dependency Injection?

Dependency injection is a software design pattern that implements inversion of control principle and allows a program to decouple dependency from hard coded to configuration based.

Two popular dependency injection frameworks are Spring and Google Guice.

Now lets talk about inversion of control –

What is Inversion of control (IoC)?

Inversion of control (IoC) is a design principle in which a dependent object is coupled to the object at run time. Dependent object will satisfy the dependency during program execution typically not known at compile time using static analysis. There will be a run-time binding, which can be done by dependency injection or a service locator.

There are different ways to implement inversion of control – like service locator pattern, dependency injection, factory pattern, Template method design pattern, strategy design pattern.

Dependency Injection Facts:

An injection is the passing of a dependency as a reference or pointer to a client object, which is then made part of its state. Passing the reference, rather than allowing clients to build or find the dependency, is the fundamental requirement of the pattern.

Other forms of inversion of control, such as the service locator pattern, allow clients to know about the system they use to find dependent objects. The prohibition from having even this slight dependency is what distinguishes dependency injection. One of dependency injection’s core principles is the separation of client behavior from dependency resolution.

Spring Framework provides Inversion of Control (IoC) container ot addresses the dependency in your application. It gives a complete configurable and maintainable dependency management for your application.

Dependency injection involves below four elements:

Parent – A client that uses the dependencies being injected into it
Dependent – The dependencies that have their references passed to the client
Contracts – The interfaces the client uses to communicate with its dependencies
Injector = An injector that passes the references

Types of Dependency Injection:

1) Constructor injection

Requires the client provide a parameter in a constructor for all dependencies to be injected this way.
AccountClient(AccountService accountservice) {
this.accountservice = accountservice;
}

2) Setter injection

Requires the client provide a setter method for each dependency.
public void setAccountService(AccountService accountservice) {
this.accountservice = accountservice;
}
3) Interface injection

This is simply the client publishing a role interface to the setter methods of the clients dependencies. It can be used to establish how the injector should talk to the client when injecting dependencies.
public interface ServiceSetter {
public void setAccountService(AccountService accountservice);
}
public class AccountClient implements AccountServiceImpl {

private AccountService accountservice;

@Override
public void setAccountService(AccountService accountservice) {
this.accountservice = accountservice;
}
}
Spring Dependency Injection using Annotations:

you can inject spring dependency through different spring annotation.

@Autowired tells the Spring IOC container to find configured object of this type and inject an instance at runtime.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class AccountClient implements AccountServiceImpl {

private AccountService accountservice;

@Autowired
public void setAccountService(AccountService accountservice) {
this.accountservice = accountservice;
}
}
}
Spring Dependency Injection using XML:

you can inject spring dependency through spring configuration files. Below example code snippet show how accountClient bean injects its dependent bean accountservice through XML configuration.
<bean id=”accountservice” class=”org.gopaldas.service.AccountService” />
<bean id=”accountClient” class=”org.gopaldas.client.AccountClient”>
<property name=”accountservices” ref=”accountservice” />
</bean>

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

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