Java Reference
In-Depth Information
CDI Hello EJB
<br/>
The message : #{myEJB.helloWorld}
Example 3 - setting a bean with a specific scope for simple
operations
For one reason or another you may want to implement the singleton pattern. In the
traditional approach, you will implement a singleton EJB type even if you do not ne-
cessarily need all of the services that such a component offers (scalability, roles-
based security, concurrency management, transaction management, and others).
With CDI, you can create your bean with the desired scope without the obligation of
implementing heavy components for marginal processing. In fact, CDI offers sever-
al types of scope that can be defined using annotations ( @ApplicationScoped ,
@RequestScoped , and @SessionScoped ). Thus, to implement the singleton pat-
tern without cluttering the services offered by the EJB components, we can simply
use the application scope annotation of the CDI as shown in the following code:
@ApplicationScoped
public class MySingletonBean {
//...
}
Example 4 - use of objects usually created by a factory
You want to send an asynchronous message via JMS from EJB. The classical ap-
proach will require you to instantiate many objects as is the case in the following
code:
@Stateless
public class SendMessageBean {
@Resource(name = " java:global/jms/
javaee7ConnectionFactory")
Search WWH ::




Custom Search