Java Reference
In-Depth Information
Who injects the dependency?
One of the nice things about dependency injection frameworks is how easy it is to not
use them—when it suits you. When code constructs all its own dependencies, it can
be difficult to bypass that code and run against stubs for testing. If dependencies are
passed in, on the other hand, all that's required is for your test code to call a setter
with a mock object of your choice.
2.3.2
Introducing the Blueprint service
Enterprise OSG i provides a simple way of accessing services declaratively, known as
Blueprint . Blueprint supports inversion of control; a component's dependencies are
injected into it rather than being looked up by it. Like almost everything in the enter-
prise OSG i programming model, Blueprint itself is implemented as a service. (It's a
service for registering and injecting services.)
DEPENDENCY INJECTION AND OSGI
Dependency injection is useful everywhere, but has special benefits in an OSG i envi-
ronment. Because the OSG i Service Registry is such a dynamic environment, it's diffi-
cult to correctly write a bundle that makes use of a service in a safe way, monitoring its
lifecycle and finding an appropriate replacement. Using and providing services
becomes even more difficult when an implementation depends upon more than one
service, with a user having to write some complex thread-safe code. Using dependency
injection eliminates this complexity by managing the lifecycle of the services exposed
and consumed by a bundle.
In Apache Aries, Blueprint is used as a core part of the OSG i programming model.
It's used not only to provide a dependency injection model, but it's also the integra-
tion point for many enterprise technologies and declarative qualities of service, such
as managed JPA and container managed transactions. We'll explain more about JPA
and JTA in the next chapter and discuss alternative OSG i dependency injection frame-
works in section 6.2.
2.3.3
Coupling two components with Blueprint
One thing that would enhance the Fancy Foods web page is a listing of special offers.
The Fancy Foods shop will have several departments, each with their own criteria for
what goes on offer. For example, prices of fresh fruit and vegetables will vary a lot
depending on the season and the weather. (In general, fruit prices and vegetable
prices move in opposite directions. Summer is best for most fruits, whereas many veg-
etables are at their best in winter. This is partly why Brussels sprouts are so popular for
Christmas dinner.) Other products don't have the same seasonal price fluctuations,
but sell better at certain times of the year. For example, it makes sense to highlight
premium chocolates on the front page around Valentine's Day, bulk candies for Hal-
loween, and pickled onions and organic turkeys before Christmas. Clearly all this logic
won't fit into HelloWorld.java , or even into a single other class.
Search WWH ::




Custom Search