Java Reference
In-Depth Information
Chapter 14. Contexts and Dependency Injection for the
Java EE Platform: Advanced Topics
This chapter describes more advanced features of Contexts and Dependency Injection for
the Java EE Platform. Specifically, it covers additional features CDI provides to enable
loose coupling of components with strong typing, in addition to those described in “Over-
view of CDI” in
The Java EE 6 Tutorial: Basic Concepts
.
The following topics are addressed here:
• “
Using Alternatives in CDI Applications
”
on page
251
• “
Using Predefined Beans in CDI Applications
” on page
256
• “
Using Events in CDI Applications
”
on page
257
• “
Using Interceptors in CDI Applications
” on page
260
• “
Using Decorators in CDI Applications
”
on page
262
• “
Using Stereotypes in CDI Applications
”
on page
263
Using Alternatives in CDI Applications
When you have more than one version of a bean you use for different purposes, you can
choose between them during the development phase by injecting one qualifier or another,
as shown in “The simplegreeting CDI Example” in
The Java EE 6 Tutorial: Basic Con-
cepts
.
Instead of having to change the source code of your application, however, you can make
the choice at deployment time by using
alternatives
.
Alternatives are commonly used for purposes like the following:
• To handle client-specific business logic that is determined at runtime
• To specify beans that are valid for a particular deployment scenario (for example,
when country-specific sales tax laws require country-specific sales tax business lo-
gic)
• To create dummy (mock) versions of beans to be used for testing