Inversion of Control in Spring

As we mentioned earlier, Inversion of Control is a big part of what Spring does, and the core of Spring's implementation is based on Dependency Injection, although Dependency Lookup features are provided as well. When Spring provides collaborators to a dependent object automatically, it does so using Dependency Injection. In a Spring-based application, it is always preferable to use Dependency Injection to pass collaborators to dependent objects rather than have the dependent objects obtain the collaborators via Lookup. Although Dependency Injection is the preferred mechanism for wiring together collaborators and dependent objects, you need Dependency Lookup to access the dependent objects. In many environments, Spring cannot automatically wire up all of your application components using Dependency Injection and you must use Dependency Lookup to access the initial set of components. When you are building web applications using Spring's MVC support, Spring can avoid this by gluing your entire application together automatically. Wherever it is possible to use Dependency Injection with Spring, you should do so; otherwise you can fall back on the Dependency Lookup capabilities. You will see examples of both in action during the course of this chapter, and we are sure to point them out when they first arise.

An interesting feature of Spring's IoC container is that it has the ability to act as an adaptor between its own Dependency Injection container and external Dependency Lookup containers. We look at this in more detail in Chapter 5.

Spring supports both constructor and setter injection and bolsters the standard IoC feature set with a whole host of useful additions to make your life easier.

The rest of this chapter introduces the basics of Spring's DI container complete with plenty of examples.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net