Java Reference
In-Depth Information
Chapter 7
Contexts and Dependency Injection
One of the most important features in Java EE is Contexts and Dependency Injection (CDI). CDI helps bind the web
tier and the business logic or transactional tier of the Java EE platform together and makes it easy to utilize resources
and objects throughout an application. CDI makes it easy to expose business objects for use within JSF web views so
that developers can directly bind JSF view widgets to public JavaBean members and methods. It also provides facilities
that make it possible to inject JavaBean classes and resources into other Java objects in a type-safe and efficient
CDI is architected from two methodologies: contexts and dependency injection. Contexts provide the ability to
bind the life cycle and interactions of stateful components to well-defined but extensive contexts, per Oracle's Java EE 7
tutorial. In the same tutorial, dependency injection is defined as the ability to inject components into an application in
a type-safe manner, including the ability to choose at deployment time which implementation of a particular interface
to inject. To use CDI, a developer should become familiar with a series of annotations that can be used to decorate
objects and injected components. This chapter covers concepts that will demonstrate such annotations and where
they should be used.
Since CDI provides a high level of loose coupling, it is an important piece of any Java EE application. Applications
that use CDI in the right way can become very efficient because CDI provides decoupling of resources, as well as
strong typing by eliminating the requirement to use String -based names for managed resources, and provides
the use of declarative Java annotations to specify just about everything. Although it is possible to develop Java EE
applications without the use of CDI, it is very easy to use and enables enterprise applications to become more robust
and efficient than those that do not use CDI features.
This chapter will provide a brief overview of the concepts that comprise CDI, but it will focus on mostly those
new features that are part of CDI 1.1 and the Java EE 7 platform. CDI 1.1 is an incremental release, which provides
many small enhancements to help solidify the API. If you are unfamiliar with CDI, I recommend you learn more
about it from the Oracle Java EE 7 tutorial online before reading this chapter. If you want to be on the cutting edge of
CDI, you can learn much more about current and future CDI features by referencing Weld, a subset of the JBoss Seam
web framework, which is the reference implementation for CDI. To learn more about Weld, please refer to the online
documentation at .
Brief Overview of CDI
While CDI is part of Java EE 7 and is certainly utilized within Java EE applications, it is by no means available only
within the Java EE ecosystem; it can also be utilized within Java SE applications. Of course, only a subset of the CDI
API, such as bean injection and bean scope, is available for use within Java SE applications. The focus of this chapter
is geared toward use within a Java EE environment; to learn more about using CDI within Java SE, please refer to the
online documentation.
CDI is comprised of a variety of concepts and programming techniques. Let's break it down just a bit so you can
more easily understand the scope of CDI. As mentioned in the introduction to this chapter, the C portion of CDI refers
to the contextual scoping of resources within an application. Inherently, resources that are managed by CDI will have
scope propagated to them in such a way that the container or application will automatically manage their scopes.
Search WWH ::

Custom Search