Java Reference
In-Depth Information
Besides these four scopes, there are also two pseudo-scopes: singleton and dependent. The
singleton scope handles the special case of singleton beans. The dependent scope is the
scope that a bean is assigned to by default if the bean isn't assigned explicitly to a scope.
An object that belongs to the dependent scope is created when the object it belongs to is
created and is destroyed when its owning object is destroyed. This might seem a little con-
fusing, but it'll be clarified as you move through this chapter.
You're not limited to the six scopes or contexts we just discussed. CDI has a pluggable
architecture and with a little code, you can develop your own contexts. For example, you
might port support for JSF's view scope into CDI.
Type-safe dependency injection
Unlike other DI frameworks, CDI doesn't use string-based identifiers to determine what
object should be injected. Instead, CDI uses type information provided by the Java object
model to determine what object should be injected. In situations where the determination
is ambiguous because multiple Java objects match the type, qualifier annotations are used
to select the correct candidate. Because CDI uses the Java type system and annotations,
there's never any doubt as to what will be injected and the types always match.
Event notification
One of the dangers with large feature-rich applications is tight coupling. There's the danger
that as the application grows it'll increasingly become a tangled mess. Injection will be per-
formed just so a listener can be registered. Registration of listeners is fraught with many
challenges, because this is server-side development where objects may be serialized and
new sessions initiated. Although injection simplifies the process of acquiring a reference to
an object, it isn't quite enough. What you want to be able to do is declare that a bean listens
for certain events and then lets the framework handle the event routing. This is accom-
plished by placing an annotation on the method that receives the event, and the container
handles the rest. This would be analogous in Swing to marking a method that processes an
ActionListenerEvent and having the listener registration performed automatically.
Interceptors
You've already seen the interceptor support provided by EJB. Interceptor support in both
EJB and CDI is specified in JSR-381 Interceptors 1.1. With CDI, interceptors aren't limited
to EJBs and can be used on any bean managed by CDI. With CDI, interceptors can be used
with business methods on a bean, as well as lifecycle and timeout callbacks. When we dive
Search WWH ::




Custom Search