img
Inversion of Control and Dependency Injection
At its core, IoC, and therefore DI, aims to offer a simpler mechanism for provisioning component
dependencies (often referred to as an object's collaborators) and managing these dependencies
throughout their life cycles. A component that requires certain dependencies is often referred to as the
dependent object or, in the case of IoC, the target. This is a rather grand way of saying that IoC provides
services through which a component can access its dependencies and services for interacting with the
dependencies throughout their life. In general, IoC can be decomposed into two subtypes: Dependency
Injection and Dependency Lookup. These subtypes are further decomposed into concrete
implementations of the IoC services. From this definition, you can clearly see that when we are talking
about DI, we are always talking about IoC, but when we are talking about IoC, we are not always talking
about DI (for example, Dependency Lookup is also a form of IoC).
Types of Inversion of Control
You may be wondering why there are two different types of IoC and why these types are split further into
different implementations. There seems to be no clear answer to this question; certainly the different
types provide a level of flexibility, but to us, it seems that IoC is more of a mixture of old and new ideas;
the two different types of IoC represent this.
Dependency Lookup is a much more traditional approach, and at first glance, it seems more
familiar to Java programmers. Dependency Injection is a newer, less well-established approach that,
although it appears counterintuitive at first, is actually much more flexible and usable than Dependency
Lookup.
With Dependency Lookup­style IoC, a component must acquire a reference to a dependency,
whereas with Dependency Injection, the dependencies are injected into the component by the IoC
container. Dependency Lookup comes in two types: Dependency Pull and Contextualized Dependency
Lookup (CDL). Dependency Injection also has two common flavors: Constructor Dependency Injection
and Setter Dependency Injection.
Note For the discussions in this section, we are not concerned with how the fictional IoC container comes to
know about all the different dependencies, just that at some point, it performs the actions described for each
mechanism.
Dependency Pull
To a Java developer, Dependency Pull is the most familiar type of IoC. In Dependency Pull,
dependencies are pulled from a registry as required. Anyone who has ever written code to access an EJB
(2.1 or prior versions) has used Dependency Pull (i.e., via the JNDI API to look up an EJB component).
Figure 4-1 shows the scenario of Dependency Pull via the lookup mechanism.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home