Java Reference
In-Depth Information
To enable an alternative, the class must be specified in the beans.xml file. As mentioned,
you'll either package using a different beans.xml file or dynamically generate one at build
time. To enable DevelopmentStartup , you'd add the following configuration entry to
the beans.xml file:
<alternatives>
<class>com.actionbazaar.setup.DevelopmentStartup</class>
</alternatives>
You now should have a pretty good handle on injection and the lifecycle of beans. It's time
to turn your attention to interceptors and decorators. After this we'll look at using stereo-
types to reduce the annotation explosion.
12.4. Interceptor and decorators
Earlier in this topic we delved into the support for interceptors added in EJB 3. While EJB
3's interceptor support is useful, it's limited to EJBs. With CDI, interceptors can be used
with any bean, not just an EJB. Like EJB 3, CDI uses annotations to drive its interceptor
implementation. Although CDI's approach isn't as powerful as AspectJ's, it's much easier
to use and with fewer complications.
CDI's support for interceptors comes in two flavors: traditional interceptors and decorators.
A traditional interceptor can be applied to any method on any bean. The interceptor isn't
specific to the bean or method it's intercepting. A traditional interceptor is what we usually
think of when we see the term interceptors . CDI also includes a new type of interceptor
called a decorator . A decorator implements the interface of the class it's intercepting—it's
thus coupled to that class and intimately tied to its implementation or business function.
Thus, a decorator knows its implementation.
A decorator is used to implement specific business functionality for an interface that's ab-
stracting into a crosscutting concern. A traditional interceptor, on the other hand, provides
generic crosscutting logic. This may appear confusing at first, but as you read through the
subsequent sections and code, it'll crystalize. Let's begin by looking at traditional inter-
ceptors.
Search WWH ::




Custom Search