JSF2.0 Tomcat7中@Named和@ManagedBean注解的区别 [英] Difference between @Named and @ManagedBean annotations in JSF2.0 Tomcat7
问题描述
这可能是一个菜鸟问题,但是在很多教程和示例中,我看到使用这些注释就好像它们做同样的事情一样.
This might be a noob question, however in a lot of tutorials and examples I saw these annotations used as if they did the same thing.
但是我在使用 @Named
时遇到了一些限制(特别是依赖注入等)我找不到解释差异的来源,如果有人我会非常感谢可以粗略地概述何时使用其中一个.
However I ran into some limitations using the @Named
one (especially with dependency injection etc.) I couldn't find a source where the difference is explained and I'd be very thankful if someone can give a rough overview on when to use one or the other.
推荐答案
@Named
为 CDI 托管 bean 提供了一个 EL 名称,以便在 JSF 或 JSP 等视图技术中使用.请注意,在 CDI 应用程序中,您不需要需要 @Named
注释来创建由 CDI 管理的 bean(感谢 @Karl 的评论).
@Named
gives a CDI managed bean an EL name to be used in view technologies like JSF or JSP. Note that in a CDI application you don't need the @Named
annotation to make a bean managed by CDI (thanks to @Karl for his comment).
@ManagedBean
使 bean 由 JSF 管理,您可以:
@ManagedBean
makes the bean managed by JSF and you can:
- 将其注入其他@ManagedBean 注释的bean(但不注入@Named bean!)
- 通过表达语言从您的观点访问它
请参阅此相关问题了解更多信息两种 bean 之间的注入是如何工作的.
See this related question for further information how injection works among both kind of beans.
请注意,bean 的范围也有所不同.它们来自不同的包但名称相同(JSF:javax.faces.bean
,CDI:javax.enterprise.context
,因此它通常是错误和混淆的来源如果您包含错误的类.
Note that there is also a difference with the scope of the beans. They come from different packages but are named identically (JSF: javax.faces.bean
, CDI: javax.enterprise.context
, so it is often a source of error and confusion if you include the wrong class.
根据我的经验:您应该尽可能使用 CDI bean,因为它们比 JSF 托管 bean 更灵活. 唯一的缺点是 CDI 不知道视图范围,因此您要么需要回退到@ManagedBean,要么使用一些第三方扩展,如 Seam.
From my experience: You should use CDI beans whenever possible since they are more flexible than JSF managed beans. Only drawback is that CDI doesn't know a view scope, so you either need to fall back to @ManagedBean or use some third party extension like Seam.
CDI 支持 ViewScope,更多信息关于 链接
CDI supports ViewScope, more info on LINK
这篇关于JSF2.0 Tomcat7中@Named和@ManagedBean注解的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!