This question already has an answer here:
-
Inversion of Control vs Dependency Injection
18 answers
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...
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
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
- Constructor Injection
- Setter/Getter Injection
- 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?
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.
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.
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.
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