What's in the Zone?

Injection and Inversion

Posted by indroneel on April 4, 2007

A lot of programmers I interact with are unable to differentiate between dependency injection and inversion of control. Thanks to the popularity of Spring framework and EJB 3.0 there is now a tendency to use these terms interchangeably, even as they represent distinctly different concepts, resulting in a lot of ambiguities.

Inversion of control has its antecedents in the Hollywood principle (don’t call us, we’ll call you) and is not a design pattern. Rather, it is a general principle that is realized in multiple design patterns. Dependency injection is one such pattern that builds on top of this principle. Inversion of control is one of the tenets for all object-oriented frameworks, but not all object-oriented frameworks provide the features of dependency injection.

Object-Oriented Frameworks

An object-oriented framework is a semi-finished application encapsulating common features that are repeated across different parts of the same application and even across applications. Common examples of such framework include J2EE containers, Apache Struts, Eclipse rich client platform and Swing GUI toolkit.

A framework-driven approach to application development usually involves integration of multiple object-oriented frameworks and creation of specific functionalities as extensions to these frameworks. Every framework provide its own extension mechanisms as pre-defined endpoints called hotspots. Hotspots are specific to a framework, usually pertaining to integration (application programming/service provider interfaces) and configuration (external metadata).

For example, the EJB 2.0 specifications define hotspots for Java bean objects to be deployed in an EJB container. This definition takes the form of base interfaces, method signature conventions and context-based references to other objects and resources.

Inversion of Control

Inversion of control is manifested at integration hotspots during communication between the static framework structure and custom application logic (implemented as extensions and plugins). A common characteristic of object-oriented frameworks is to maintain overall control of all activities within the hosted application. The primary objective of application modules is thereby restricted to provide functionalities that can be invoked by the framework. Compare this to a scenario without frameworks, in which there is a significant effort on how the modules can invoke and manage each other, effectively transferring control during the process. This change in focus, and the low-level design changes it entails, is what inversion of control is all about.

When deployed on top of an object-oriented framework, application modules should not directly access each other’s capabilities. The same is also true for invocations from external entities. Instead, every request must be routed through the hosting framework. The latter, in turn, can make multiple calls across more than one module in a controlled fashion before returning a result back to the caller.

In the light of what has been said so far, inversion of control can be summed up as:

“Always expect to be called. If you ever need to call, never call direct. Always ask the framework. The framework knows.”

Service Locators

Modules and components that constitute an application never exist in isolation. They need to refer to each other to execute their respective functionalities.

In a framework-driven scenario, modules access each other’s capabilities indirectly through the framework. Typically, this involves querying the framework to first obtain a reference to the target module (this process is better known as the service locator pattern). Subsequent method invocations are then made with respect to this reference.

There are several problems with the service locator approach. They are as listed below:

  • Each module that originates a call must be hand-coded with the query information (unique identifiers) for the target modules.
  • The module implementation needs to include the extra logic involved to perform lookups for target modules.
  • The query information and operations are specific to the framework. This makes the modules less portable across frameworks.

This is where dependency injection comes in handy.

Dependency Injection

In many respects, dependency injection is an antithesis to the service locator pattern. Under dependency injection, modules no longer lookup each other to invoke specific functionalities. Instead, each module ‘indicates’ its preferences for a choice of capabilities in collaborating modules. The indication mechanism usually varies depending on the capabilities of the underlying programming language.

For example, dependency injection under Java usually relies on method signature convention and annotations using a language capability called introspection. Constructors with multiple parameters and bean-style setter methods are taken to be injection points by convention. Ad-hoc methods with one or more parameters can be annotated to be recognized as injection points.

The wiring among different modules is performed by an external entity called the dependency injector. In a framework-based environment, the framework itself plays the role of injector, instantiating invoking the glue-code on modules as necessary.

What Causes the Confusion?

It is a common practice to provide the features of dependency injection as part of much larger object-oriented frameworks. While extolling the advantages of such frameworks, it is natural to stress on the inversion of control principles (the fact that this is a common characteristic across all frameworks is seldom mentioned). Somewhere down the line, the distinction between the two gets blurred with end users. This is further compounded by the fact that dependency injection builds on top of inversion of control making it impossible to separate the two.

Of special mention in this regards is the Spring framework that is repeated touted as an inversion of control container on multiple forums. While this is factually correct, a more apt description should highlight its dependency injection features to avoid confusion.


[1] Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler

[2] Stefano’s Linotype: Origin of the Hollywood Principle sheds some light on the origin of Inversion of Control.

[3] Object-Oriented Application Frameworks by Mohamed Fayad and Douglas C. Schmidt.


12 Responses to “Injection and Inversion”

  1. Seema Manivannan said


    Thanks for the interesting information. I didn’t know that the ServiceLocator pattern comes under Inversion of Control. I always thought that ServiceLocator antipattern has an alternative in Inversion of Control.


  2. Neel,

    Good write up and nice details – well “annotated” with examples as well 🙂


  3. […] Dependency injection frameworks and Dependency injection metaframeworks seem to be in thing. I had earlier expressed my […]

  4. Ameet said


    Thanks for the very well written article.


  5. Bharadwaz said

    As over all understanding i got something like this. IOC is a framework that process request interceptor and routs the request on external events.

    DI is is injecting object through certain framework API.

  6. Umut Utkan said


    Thanks for the article –that clears up all the confusion– with well-chosen examples.


  7. […] clears all the fuss and the confusion about the terms. But i came around a very good article ‘Injection and Inversion‘, about the very same subject that states all the details and the differences between these […]

  8. Hi!

    This is a really good article that differentiates between Dependency Injection and Inversion of Control. I’m most definitely linking to it and recommending it to my co-workers! 🙂


    PS: I like the illustrations. What did you use to create them?

  9. C. Marius said

    Excellent article, simple and concise!

    Thank you,
    C. Marius

  10. pravee said


  11. […] Artigos Relacionados:Nenhum artigo relacionado. […]

  12. […] What’s the difference between Dependency Injection and Inversion of Control […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: