JSF 2.0 中定义受管 Bean 的三种途径的比较

JSF 2.0 中定义受管 Bean 的三种途径的比较 Comparison of Three Ways to Define Managed Beans in JSF 2.0
JSF 2.0 大量采用标注,从而使 web/WEB-INF/faces-config.xml 不再必需。本文介绍并比较了三种途径来定义可从页面上的 EL 表达式中引用的受管 Bean。 JSF 2.0 heavily adopts annotations, so web/WEB-INF/faces-config.xml becomes optional. This article introduces and compare three ways to define a managed bean that can be referred in an EL expression on the page.
现在假设你有下面这个 Bean 类,并想让它受管于会话范围: Now assum you have a bean class like this and want it to be managed in session scope:
  1. public class MyManagedBean implements Serializable {...}
在 JSF 2.0 之前,你需要将它配置到 web/WEB-INF/faces-config.xml 中: Prior to JSF 2.0, you need to configure it in web/WEB-INF/faces-config.xml:
  1. <managed-bean>
  2.     <managed-bean-name>mmb</managed-bean-name>
  3.     <managed-bean-class>com.mycom.MyManagedBean</managed-bean-class>
  4.     <managed-bean-scope>session</managed-bean-scope>
  5. </managed-bean>
这不仅枯燥,而且难以管理、容易出错。faces-config.xml 不仅用于配置受管 Bean,还用于导航规则。如果你在做一个大的互联网项目,该文件可能会增大到上千行。你还可能会犯拼写错误,例如写错 Bean 类名,使你直至运行时才意识到问题。显然在 JSF 2.0 中,这不是优先的途径。 This is not only tedious but also hard to manage and error prone. faces-config.xml is not only used for configuring managed beans, but also for navigation rules. If you're working with a big web project, this file can grow up to one thousand lines. It's also possible for you to misspell some words, for example the bean class name, that makes you unaware the problem until at runtime. Obviously this way shouldn't be preferred in JSF 2.0.
第二种途径由 JSR 314,也就是 JavaServer Faces 2.0 本身提供。你只需将两个标注应用到类定义上: The second way is provided by JSR 314, that is JavaServer Faces 2.0 itself. You just apply two annotations onto the class definition:
  1. @javax.faces.bean.ManagedBean(name = "mmb")
  2. @javax.faces.bean.SessionScoped
  3. public class MyManagedBean implements Serializable {...}
没什么特别的,但直截了当!用这些标注,你就可以从此避免 XML 配置的枯燥和不小心拼错字。然而,这并不是最佳途径,因为 JSR 299 中定义的 CDI(用于 Java™ EE 平台的上下文和依赖注入)提供了类似且强大得多的标注: Nothing special but just nice and straightforward! With these annotations, you're set free from the tedium of XML configuration and carelessness of typo. However, this is still not the best way, because CDI specified in JSR 299 (Contexts and Dependency Injection for the Java™ EE platform) offers similar and far more powerful annotations:
  1. @javax.inject.Named("mmb")
  2. @javax.enterprise.context.SessionScoped
  3. public class MyManagedBean implements Serializable {...}
为什么要优先采用 CDI?因为正如其名,它是用于依赖注入的通用框架,也就是说它不但可用于 JSF,还能用于任何其他的 Java EE 6 技术,而且我听说 Spring 3.0 也将支持它。详细的文档可从 Weld 主页找到,这里 Weld 是 JSR 299 的参考实现。 Why should we prefer CDI? Beacuse as named it's a general purposed framework for dependency injection, which means it can not only used with JSF, but also with any other Java EE 6 tenologies, and I've heard Spring 3.0 will support it too. Detailed documentations can be found at Weld Home, where Weld is the reference implementation for JSR 299.

你可能感兴趣的:(JSF 2.0 中定义受管 Bean 的三种途径的比较)