JSF 2.0 中定義受管 Bean 的三種途徑的比較 |
Comparison of Three Ways to Define Managed Beans in JSF 2.0 |
JSF 2.0 大量采用標(biāo)注,從而使 web/WEB-INF/faces-config.xml 不再必需。本文介紹并比較了三種途徑來定義可從頁面上的 EL 表達(dá)式中引用的受管 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. |
現(xiàn)在假設(shè)你有下面這個(gè) Bean 類,并想讓它受管于會話范圍: |
Now assum you have a bean class like this and want it to be managed in session scope: |
- 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 : |
- <managed-bean>
- <managed-bean-name>mmb</managed-bean-name>
- <managed-bean-class>com.mycom.MyManagedBean</managed-bean-class>
- <managed-bean-scope>session</managed-bean-scope>
- </managed-bean>
|
這不僅枯燥,而且難以管理、容易出錯(cuò)。faces-config.xml 不僅用于配置受管 Bean,還用于導(dǎo)航規(guī)則。如果你在做一個(gè)大的互聯(lián)網(wǎng)項(xiàng)目,該文件可能會增大到上千行。你還可能會犯拼寫錯(cuò)誤,例如寫錯(cuò) Bean 類名,使你直至運(yùn)行時(shí)才意識到問題。顯然在 JSF 2.0 中,這不是優(yōu)先的途徑。 |
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 本身提供。你只需將兩個(gè)標(biāo)注應(yīng)用到類定義上: |
The second way is provided by JSR 314, that is JavaServer Faces 2.0 itself. You just apply two annotations onto the class definition: |
- @javax.faces.bean.ManagedBean(name = "mmb")
- @javax.faces.bean.SessionScoped
- public class MyManagedBean implements Serializable {...}
|
沒什么特別的,但直截了當(dāng)!用這些標(biāo)注,你就可以從此避免 XML 配置的枯燥和不小心拼錯(cuò)字。然而,這并不是最佳途徑,因?yàn)?JSR 299 中定義的 CDI(用于 Java™ EE 平臺的上下文和依賴注入)提供了類似且強(qiáng)大得多的標(biāo)注: |
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: |
- @javax.inject.Named("mmb")
- @javax.enterprise.context.SessionScoped
- public class MyManagedBean implements Serializable {...}
|
為什么要優(yōu)先采用 CDI?因?yàn)檎缙涿怯糜谝蕾囎⑷氲耐ㄓ每蚣埽簿褪钦f它不但可用于 JSF,還能用于任何其他的 Java EE 6 技術(shù),而且我聽說 Spring 3.0 也將支持它。詳細(xì)的文檔可從 Weld 主頁找到,這里 Weld 是 JSR 299 的參考實(shí)現(xiàn)。 |
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. |