- Mastering JavaServer Faces 2.2
- Anghel Leonard
- 581字
- 2021-12-08 12:41:30
JSF scopes versus CDI scopes
Even a JSF beginner might have heard about JSF managed beans (regular JavaBeans classes managed by JSF) and CDI beans (regular JavaBeans classes managed by CDI), and knows that JSF supports JSF scopes and CDI scopes. Starting with Java EE 6, CDI is recognized as the managed bean framework, besides EJBs. This causes confusion among programmers, because EJBs, CDIs, and JSF managed beans raise a critical question: which one to use and when?
Focusing on JSF, the unanimous answer is that CDI beans are more powerful than JSF beans. But, when you know right from the start that CDI will not be a part of your application or you are running the application inside a servlet container (which does not have CDI support by default, like Apache Tomcat), then JSF beans is the right choice. In other words, when you need a simple way to define beans and a neat mechanism for a dependency injection, then JSF bean will do the job, but when you need heavy artillery, such as events, type safe injection, automatic injection, producer methods, and interceptors, then CDI will represent the complete solution.
Moreover, NetBeans IDE 8.0 warns us that the JSF bean's annotations will be deprecated in the next JSF version, while the CDI beans are recommended instead (as shown in the following screenshot). This warning and the new JSF 2.2 flow scope, introduced as a dependency on CDI, are powerful signals that JSF and CDI become closer and closer:
Note
CDI beans are much powerful than JSF beans; therefore, use CDI beans whenever possible.
So, strong arguments indicate CDI is often the right choice, but there are still instances where it is effective to use JSF beans, as you will soon discover.
JSF bean's main annotations (such as @ManagedBean
and scopes annotations) are defined in the package javax.faces.bean
, while CDI's main annotations are defined in the javax.inject
(such as, @Named
) and javax.enterprise.context
(such as, scopes) packages.
A JSF managed bean is annotated with @ManagedBean
, which allows us to inject it in to another bean (not CDI beans!) and to access the bean properties and methods from JSF pages using EL expressions. A CDI bean is annotated with @Named
, which provides an EL name to be used in view technologies, such as JSP or Facelets.
Typically, a JSF bean is declared as shown in the following code:
package package_name; import javax.faces.bean.ManagedBean; import javax.faces.bean.jsfScoped; @ManagedBean @jsfScoped public class JSFBeanName { ... }
The JSF bean, @ManagedBean
, supports an optional parameter, name
. The provided name can be used to reference the bean from JSF pages in the following manner:
@ManagedBean(name="custom name")
A CDI bean has the same shape, with different annotations, as shown in the following code:
package package_name; import javax.inject.Named; import javax.enterprise.context.cdiScoped; @Named @cdiScoped public class CDIBeanName { ... }
The CDI bean, @Named
, supports an optional parameter, value
. The provided name can be used to reference the bean from JSF pages in the following manner:
@Named(value="custom name")
Note
Notice that CDI annotations cannot be mixed with JSF annotations in the same bean, only in the same application. For example, you cannot define a bean using @ManagedBean
and a CDI scope (or any other combination between them), but you can have, in the same application, a managed bean (or more) and a CDI bean (or more).
In the following figure, you can see a short overview of JSF 2.2 scopes:
In the next section, you will see how each JSF/CDI scope works.