28

IoC and DI concept were very much confusing for me so I read lots of views and post of different people and finally reach to the conclusion. So as I understand these topics are...

Inversion of control is a technique in java for making loosely coupled and more easily maintainable applications, and dependency injection is a one of the way to achieve this concept(technique).

Inversion of control container inject dependency at run time from java classes to make loosely coupled application.

Is this true ? correct me if I am wrong...

0

6 Answers 6

24

These are patterns to achieve loose coupling in java programming

DI(Dependency Injection):
Dependency injection is a pattern used to create instances of objects that other objects rely upon without knowing at compile time which class will be used to provide that functionality or simply the way of injecting properties to an object is called dependency injection.

We have three types of Dependency injection

  1. Constructor Injection
  2. Setter/Getter Injection
  3. Interface Injection

Spring will support only Constructor Injection and Setter/Getter Injection.

IOC(Inversion Of Control):
Giving control to the container to create and inject instances of objects that your application depend upon, means instead of you are creating an object using the new operator, let the container do that for you. Inversion of control relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality

The two concepts work together in this way to allow for much more flexible, reusable, and encapsulated code to be written. As such, they are important concepts in designing object-oriented solutions.

Example for Dependency injection

Previously we are writing code like this

Public MyClass{
 DependentClass dependentObject
 /*
  At somewhere in our code we need to instantiate 
  the object with new operator  inorder to use it or perform some method.
  */ 
  dependentObject= new DependentClass();
  dependentObject.someMethod();
}

With Dependency injection, the dependency injector will take off the instantiation for us

Public MyClass{
 /* Dependency injector will instantiate object*/
 DependentClass dependentObject

 /*
  At somewhere in our code we perform some method. 
  The process of  instantiation will be handled by the dependency injector
 */ 

  dependentObject.someMethod();
}

The above process of giving the control to some other (for example the container) for the instantiation and injection can be termed as Inversion of Control

You can read more on dependency injection and IOC in my answer :- You can find advantages and applications of the concepts here.

What is dependency injection?

4
  • I have a query if there is two class having definition of DependentClass interface, then which definition catch by IOC(DI)?
    – ankit
    Mar 10, 2018 at 18:15
  • If I understand you correctly, you are asking about if a class has 2 dependency ie as per our example DependentClass1 dependentObject1 and DependentClass2 dependentObject2 then both these classes where instantiated by dependency injector
    – SAMUEL
    Mar 11, 2018 at 2:38
  • No, I mean that if both DependentClass1 dependentObject1 and DependentClass2 dependentObject2 having implementation of a interface ABCInterface then when I use @Autowired on ABCInterface, then whose definition is took by container?
    – ankit
    Mar 11, 2018 at 8:17
  • That depends on how the dependency injector is implemented... Which dependency injector are you using
    – SAMUEL
    Mar 12, 2018 at 4:44
23

IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application.

DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object will 'depend' on in order to behave correctly.

Reference : Inversion of Control vs Dependency Injection

1

Inversion of control means the program delegates control to someone else who will drive the flow IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC. IOC is a concept where the flow of application is inverted. The control of the logic which is not part of that entity is taken by someone else. DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected. The biggest benefit achieved by the above approach is “Decoupling”, we can invoke an object and pass any object keeping objects independent improving reusability and maintenance.

1

IOC stands for "Inversion of Control". It will works based on IOC principle,it means collaborating the objects and managing the objects of life cycle.Collaborating means the objects are group together at one place. we can collaborating the objects are two ways which they are Dependency Pull and Dependency Injection . DP divided into two ways (Dependency pull and Contextual Dependency pull) and DI also divided into two ways (Setter Injection and Constructor Injection).IOC supports both the ways but,the main intention of spring frame work is our components will be come completely loosely coupled. If we will use Dependency pull our component class will become tightly coupled with another class even though as part of spring frame work.then the recommended to go for Dependency Injection (Setter Injection or Constructor Injection). There is no difference between the IOC and DI.IOC is different and DI is different, DI is one of the part of IOC.

0

Inversion of Control (IoC) refers to a programming style where a framework or runtime, controls the program flow. Inversion of control means we are changing the control from normal way. It works on Dependency Inversion Principle.

DI is a software design pattern that allow us to develop loosely coupled code. DI is a great way to reduce tight coupling between software components. DI also enables us to better manage future changes and other complexity in our software. The purpose of DI is to make code maintainable.

0

Inversion of Control and Dependency Injection is a core design pattern of Spring framework. IOC and DI design pattern is also a popular design pattern interview question in Java. As the name suggest Inversion of control pattern Inverts responsibility of managing the life cycle of the object e.g. creating an object, setting their dependency etc from application to a framework, which makes writing Java application even more easy.

Read more: http://javarevisited.blogspot.com/2012/12/inversion-of-control-dependency-injection-design-pattern-spring-example-tutorial.html#ixzz4xve86pPN

Not the answer you're looking for? Browse other questions tagged or ask your own question.