img
Injection vs. Lookup
Choosing which style of IoC to use--injection or lookup--is not usually a difficult decision. In many
cases, the type of IoC you use is mandated by the container you are using. For instance, if you are using
EJB 2.1 or prior versions, then you must use lookup-style IoC (via JNDI) to obtain the EJB from the JEE
container. In Spring, aside from initial bean lookups, your components and their dependencies are
always wired together using injection-style IoC.
Note When you are using Spring, you can access EJB resources without needing to perform an explicit lookup.
Spring can act as an adapter between lookup and injection-style IoC systems, thus allowing you to manage all
resources using injection.
The real question is this: given the choice, which method should you use, injection or lookup? The
answer to this is most definitely injection. If you look at the code in Listings 4-4 and 4-5, you can clearly
see that using injection has zero impact on your components' code. The Dependency Pull code, on the
other hand, must actively obtain a reference to the registry and interact with it to obtain the
dependencies, and using CDL requires your classes to implement a specific interface and look up all
dependencies manually. When you are using injection, the most your classes have to do is allow
dependencies to be injected using either constructors or setters.
Using injection, you are free to use your classes completely decoupled from the IoC container that is
supplying dependent objects with their collaborators manually, whereas with lookup, your classes are
always dependent on the classes and interfaces defined by the container. Another drawback with lookup
is that it becomes very difficult to test your classes in isolation from the container. Using injection,
testing your components is trivial, because you can simply provide the dependencies yourself using the
appropriate constructor or setter.
Note For a more complete discussion of testing using Dependency Injection and Spring, refer to Chapter 19.
Lookup-based solutions are, by necessity, more complex than injection-based ones. Although
complexity is nothing to be afraid of, we question the validity of adding unneeded complexity to a
process as core to your application as dependency management.
All of these reasons aside, the biggest reason to choose injection over lookup is that it makes your
life easier. You write substantially less code when you are using injection, and the code that you do write
is simple and can, in general, be automated by a good IDE. You will notice that all of the code in the
injection samples is passive, in that it doesn't actively try to accomplish a task. The most exciting thing
you see in injection code is objects getting stored in a field only; no other codes were involved in pulling
the dependency from any registry or container. Therefore, the code is much simpler and less error
prone. Passive code is much simpler to maintain than active code, because there is very little that can go
wrong. Consider the following code taken from Listing 4-4:
public void performLookup(Container container) {
this.dependency = (Dependency) container.getDependency("myDependency");
}
In this code, plenty could go wrong: the dependency key could change, the container instance could
be null, or the returned dependency might be the incorrect type. We refer to this code as having a lot of
moving parts because plenty of things can break. Using Lookup might decouple the components of your
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home