Java Reference
In-Depth Information
Contexts
The concept of context is easiest to understand by looking at your typical e-commerce web
application. Most web applications have at least two contexts or scopes: application and
session. The data stored in the application scope is shared—it isn't tied to one particular
user. An example of this would be data that cached for a website landing page. This page
is accessible to all users, and it doesn't make sense to access the database for the same in-
formation for each visitor. Session scope , on the other hand, is associated with a specific
user—typically a browser window. A typical example of a session scope is a virtual shop-
ping cart. The shopping cart contains the items specific to each visitor. The application state
associated with the user is associated/tracked using either cookies or IDs appended to the
URL. Application and session scopes are different types of contexts.
We've just looked at two different contexts that you have in your typical web application.
But if you step back, you'll realize that there's more than one context in your applica-
tion—there are actually many. With the advent of tabbed web browsers, a user may be
browsing two different parts of your website. In the case of a travel website, a user might
have two tabs open and be comparing flights to Rome on two different days or comparing
two different hotels. The user thus has two different contexts that must be tracked separ-
ately. In addition, the user may have created a nested context by launching a wizard in one
window to not only pick a hotel but also book a car at the same time. As you can see, ap-
plication scope and session scope are suddenly not enough; you need additional contexts
with which to build your applications.
This is where CDI comes into play. It introduces additional contexts and provides a simple
mechanism by which you can define additional contexts. Because CDI is also a container, it
manages your objects being fully aware of the context the object is associated with. Objects
are bound to contexts and their lifecycle is tied to the context. This is extremely powerful;
the container is now doing a lot of the heavy lifting that used to be the responsibility of the
application.
CDI comes with four built-in contexts or scopes:
• Application
• Conversation
• Request
• Session
Search WWH ::




Custom Search