Java Reference
In-Depth Information
}
}
From a JSF page, we can call the method
myEjbHelloWorld
.
Hello EJB
<br/>
The message : #{myManagedBean.myEjbHelloWorld}
With CDI, we do not necessarily need to go through a managed bean to access the
methods of an EJB. In fact, we only need to add the
@Named
annotation to our EJB
component and it will be accessed from our JSF page like a simple JSF-managed
bean. The difference between the two annotations (
@Named
and
@ManagedBean
) is
visible in at least two points: the first point concerns the scope. Indeed, the
@Man-
agedBean
annotation is specific to the JSF Specification while the
@Named
annota-
tion can create managed beans accessible to a greater number of specifications (in-
cluding JSF) and provides more flexibility in the handling of JavaBean components.
The second point relates to the features available to the component. The
@Named
annotation allows you to create CDI beans, which gives you the opportunity to use
features that you will not have access to in a JSF bean, such as: interceptors,
Pro-
ducer
, and
Disposer
. As a general rule, it is advisable to use CDI beans whenever
possible.
The following code shows an EJB component annotated with CDI
@Named
annota-
tion:
@Named
@Stateless
public class MyEJB implements IMyEJB {
//...
}
The following code shows the access to an EJB from a JSF page: