Java Reference
In-Depth Information
The selected item is injected into this controller . The conversation is also injected
. The confirmBid method prints out the conversation ID for debugging purposes . It
also terminates the conversation once the bid has been successfully persisted. Objects
residing in this conversation will be released.
In this example from ActionBazaar, the selected item has been stored in the conversational
scope using a producer method. It's then accessed from two pages: the item page, which
enables the user to view the item and make a bid, and the place bid page, where the user can
review the bid and give final approval. This is just one example using the conversational
scope—this is much easier than attempting to roll your own solution.
We've explored the major features of CDI. It's now time to wrap up our discussion on CDI
with pointers on how to use it effectively with EJB 3.
12.8. Using CDI effectively with EJB 3
After reading this chapter about all of the exciting new features of CDI, you may be won-
dering where EJBs fit into the picture and how CDI can be effectively paired with EJB 3.
You might also be questioning the relevance of EJBs and whether CDI is an EJB replace-
ment. In this section we'll attempt to answer these questions and provide guidance with
regard to architecting a Java EE application.
As mentioned earlier, CDI was added in Java EE 6 and its integration has continued to
grow with Java EE 7. EJB and CDI are complementary technologies that enable one an-
other and enable robust Java EE apps to be built relatively easily. CDI greatly simplifies
the amount of work necessary to use EJBs—there's no need to access JNDI or write a cus-
tom JNDI abstraction layer. CDI introduces dependency injection and context features as
well as some extensions to POJOs. CDI isn't a replacement for EJBs—EJBs are necessary
when security, transactions, remoting, scheduling, asynchronous support, and locking are
required. Don't forget, however, that EJBs are beans within CDI, so you can approach the
problem as if everything is a CDI bean, and you upgrade a CDI bean to an EJB when you
require the Enterprise services provided by the EJB container.
To effectively use CDI and EJB, it's best to examine the sample code in this chapter. Al-
though ActionBazaar is a tiny application with some sections contrived and compressed
Search WWH ::




Custom Search