支持 bean (@ManagedBean) 或 CDI Beans (@Named)? [英] Backing beans (@ManagedBean) or CDI Beans (@Named)?

查看:34
本文介绍了支持 bean (@ManagedBean) 或 CDI Beans (@Named)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始阅读Core JavaServer Faces,第 3 版,他们这样说(强调我的):

I've just started reading through Core JavaServer Faces, 3rd Ed. and they say this (emphasis mine):

有两个独立的机制,CDI bean,这是一个历史意外和 JSF 托管 bean,用于可在 JSF 页面中使用的 bean.我们建议除非您的应用程序必须在普通 servlet 上工作,否则您使用 CDI bean跑者如Tomcat.

It is a historical accident that there are two separate mechanisms, CDI beans and JSF managed beans, for beans that can be used in JSF pages. We suggest that you use CDI beans unless your application must work on a plain servlet runner such as Tomcat.

为什么?他们不提供任何理由.我一直在将 @ManagedBean 用于在 GlassFish 3 上运行的原型应用程序中的所有 bean,但我并没有真正注意到这方面的任何问题.我并不特别介意从 @ManagedBean 迁移到 @Named,但我想知道我为什么要麻烦.

Why? They don't provide any justification. I've been using @ManagedBean for all the beans in a prototype application running on GlassFish 3, and I haven't really noticed any issues with this. I don't especially mind migrating from @ManagedBean to @Named, but I want to know why I should bother.

推荐答案

CDI 优于普通 JSF,因为 CDI 允许 JavaEE 范围的依赖注入.您还可以注入 POJO 并让它们受到管理.使用 JSF,您只能注入 CDI 的一部分.

CDI is preferred over plain JSF because CDI allows for JavaEE-wide dependency injection. You can also inject POJOs and let them be managed. With JSF you can only inject a subset of what you can with CDI.

这篇关于支持 bean (@ManagedBean) 或 CDI Beans (@Named)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆