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

Inversion of Control is a pattern where the controls of dependencies are moved out usually from program components to container. The usual control flow of a program is inverted, and hence it is called so. 

For instance, instead of using a new keyword to create an object and setting dependencies, a dependency injection framework (e.g. spring container) will create the object and its dependencies for you at runtime and inject them into your code. 

The process of injecting the dependencies of your class from outside of the class is called dependency injection. The dependencies (including transitive dependencies) are usually injected into code by a dependency injection framework like the Spring framework. To make use of this flexibility better, you have to program to abstraction, and allow the framework to inject the actual instance at runtime.

Inversion of control (IoC) may also be done using dependency lookup. Dependency lookup is the looking up of dependencies using container provided services, like a JNDI lookup. 

Implicit dependency injection is always preferred over explicit dependency lookup. This is because, the application code will be free of any lookup code which usually depend on container APIs, and hence can be moved across containers.

 

Important points about Inversion of Control (IoC) can be summarized as follows:

  1. Primary advantage of inversion of control is that you can change one implementation to another implementation easily if you have also coded to interfaces.

  2. Inversion of Control (IoC) in containers can be through dependency lookup and dependency injection, though IoC is usually understood as dependency injection.

  3. Inversion of Control (IoC) and Dependency Injection (DI) leads to loose coupling. Loose coupling is always desirable. 

 

Important points on Dependency Injection can be summarized as follows:

Dependency injection can be Constructor Injection (injecting through constructors) and/or Setter Injection (injecting through setters).

Setter Injection

  • Primary advantage of Setter Injection is that it allows re-configuring of the components after its creation.

  • Disadvantages of Setter injection include:

    1. may leave the object in an incomplete state if all dependencies are not injected before use.

    2. it might not be suitable if the order of invocation of setter methods is important.

Constructor Injection

  • Primary advantage of constructor injection is that component will be in a consistent state and ready to use immediately after its creation.

  • Disadvantage of constructor injection include:

    1. We will not be able to reconfigure the component, unless setters are also present.

    2. Constructor injection cannot handle circular dependency whereas setter injection can handle circular dependency: if two objects depend on the other through constructor injection, both objects will not be created as both cannot be created without the other.

 

More Reading

If you want to explore more on this topic, please read post written by Martin Fowler on 'Inversion of Control Containers and the Dependency Injection pattern'. Please note that some of the content might even contradict the definitions given above. 

Tags: 
JavaJee Checks: 
Plagiarism Check last done on 6/June/2015.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)