Improved testability: When you design your classes for DI, you make it possible to
·
replace dependencies easily. This is especially handy when you are testing your
application. Consider a business object that performs some complex processing;
for part of this, it uses a DAO to access data stored in a relational database. For
your test, you are not interested in testing the DAO; you simply want to test the
business object with various sets of data. In a traditional approach, where the
business object is responsible for obtaining an instance of the DAO itself, you have
a hard time testing this, because you are unable to replace the DAO
implementation easily with a mock implementation that returns your test data
sets. Instead, you need to make sure your test database contains the correct data
and uses the full DAO implementation for your tests. Using DI, you can create a
mock implementation of the DAO object that returns the test data sets, and then
you can pass this to your business object for testing. This mechanism can be
extended for testing any tier of your application and is especially useful for testing
web components where you can create mock implementations of
HttpServletRequest and HttpServletResponse.
Fostering good application design: Designing for DI means, in general, designing
·
against interfaces. A typical injection-oriented application is designed so that all
major components are defined as interfaces, and then concrete implementations
of these interfaces are created and hooked together using the DI container. This
kind of design was possible in Java before the advent of DI and DI-based
containers such as Spring, but by using Spring, you get a whole host of DI features
for free, and you are able to concentrate on building your application logic, not a
framework to support it.
As you can see from this list, DI provides a lot of benefits for your application, but it is not without
its drawbacks. In particular, DI can make it difficult for someone not intimately familiar with the code to
see just what implementation of a particular dependency is being hooked into which objects. Typically,
this is only a problem when developers are inexperienced with DI; after becoming more experienced
and following good DI coding practice (e.g., putting all injectable classes within each application layer
into the same package), developers will be able to discover the whole picture easily. For the most part,
the massive benefits far outweigh this small drawback, but you should consider this when planning your
application.
Beyond Dependency Injection
The Spring core alone, with its advanced DI capabilities, is a worthy tool, but where Spring really excels
is in its myriad of additional features, all elegantly designed and built using the principles of DI. Spring
provides features for all layers of an application, from helper application programming interfaces (APIs)
for data access right through to advanced Model View Controller (MVC) capabilities. What is great about
these features in Spring is that, although Spring often provides its own approach, you can easily
integrate them with other tools in Spring, making these tools first-class members of the Spring family.
Aspect-Oriented Programming with Spring
Aspect-oriented programming (AOP) is one of the "programming models of the moment" in the Java
space. AOP provides the ability to implement crosscutting logic--that is, logic that applies to many parts
of your application--in a single place and to have that logic applied across your application
automatically. AOP is enjoying an immense amount of time in the limelight at the moment; however,
behind all the hype is a truly useful technology that has a place in any Java developer's toolbox.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home