Java Reference
In-Depth Information
into implementation details, you'll see how CDI has used annotations for marking classes
and methods that should be intercepted.
Related to interceptors, CDI also introduces a new construct called decorators . A decorator
is an interceptor that's tied to a specific interface. The decorator implements the interface it
will be intercepting. Thus, a decorator is an interceptor that provides interception that's tied
to the specific business logic. A decorator, unlike an interceptor, has intimate knowledge of
the class it's intercepting. An interceptor is a generic solution for crosscutting across dis-
parate classes, whereas a decorator is for crosscutting on a specific inheritance hierarchy.
Now that you have a basic handle on the major features of CDI, it's time to turn your at-
tention to the relationship between CDI and EJB 3. After all, this is a book on EJB, so we
know you're interested in how the two technologies complement each other.
12.1.2. Relationship between CDI and EJB 3
The relationship between EJB and CDI can be confusing at first. Both are object containers
and so there's some overlap in terms of functionality. EJB beans are still managed by the
EJB container. Thus, the EJB container handles transactions and concurrency and provides
all of the supporting functionality that we covered in earlier chapters. CDI manages its own
beans but also provides services to EJBs. The two aren't competing technologies but are
instead complementary. You can think of CDI beans as object containers that provide in-
jection, events, interceptors, and scoping support to objects that don't need the full set of
services provided by the EJB container. Throughout this topic we've delineated the situ-
ations where you should use an EJB—where you need transaction support, security, and so
on. CDI is the container for your POJOs.
CDI and EJB are fully integrated. This means that EJBs can use all of the services we just
discussed including dependency injection, event notification, interceptors, and decorators.
These services are available to all EJB types: singleton, stateless, stateful, and message-
driven beans. Because there's an overlap in services, you might be wondering when you
should use the EJB services (injection, interceptors, and so on) versus their CDI counter-
parts. The simple answer is to use the CDI variant because they're more powerful, generic,
and not limited to just EJBs. As you'll see, you can safely use the @Inject instead of the
@EJB .
Search WWH ::

Custom Search