本文介绍了由于CDI / Weld中的@Named,@ ManagedBeans在JavaEE6中是否已过时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于CDI(及其实现Weld),JEE6中的每个POJO都可以使用 @Named 进行注释,这使得POJO可以被视图访问。

Because of CDI (and its implementation Weld), every POJO in JEE6 can be annotated with @Named, which makes the POJO accessible to the view.

这是否意味着ManagedBeans现在已经完全过时了?
或者我错过了哪些 @ManagedBean 仍然有意义?

Does that mean that ManagedBeans are completely obsolete now? Or did I miss something where @ManagedBean still makes sense?

推荐答案

简而言之, @ManagedBean 对于使用JSF但不使用JSR 299的应用程序有意义(无论原因是什么)。下面是Gavin King的更长解释:

In short, @ManagedBean makes sense for applications that use JSF but do not use JSR 299 (whatever the reason is). Below a longer explanation from Gavin King:

这是一个很好的问题,并且我不是
真的完全同意到目前为止发布的
答案。

It's a good question, and I'm not really in full agreement with the answers that have been posted so far.

新的EE Managed Beans规范

Java EE定义了一个基本组件模型,以及一组非常基本的
容器服务( @Resource
@PostConstruct @PreDestroy )。

The new EE Managed Beans specification defines a base component model for Java EE, together with a very basic set of container services (@Resource, @PostConstruct, @PreDestroy).

这个想法是其他规范
(从EJB,CDI,JSF和
新的Java拦截器规范开始)构建在
这个基本组件模型和层
附加服务上,例如
事务管理,类型安全
依赖注入,拦截器。因此,这个级别的
,托管bean,CDI,
拦截器和EJB规范
都是同时工作的,并且是高度
互补的。

The idea is that other specifications (beginning with EJB, CDI, JSF and the new Java Interceptors spec) build upon this base component model and layer additional services, for example transaction management, typesafe dependency injection, interceptors. So at this level, the managed beans, CDI, interceptors and EJB specifications all work hand-in-hand and are highly complementary.

现在,Managed Beans规范
相对于
非常开放,确定哪些类是
托管bean。它确实提供
@ManagedBean 注释作为一个
机制,但它也允许其他
规范定义不同的
机制。所以,例如:

Now, the Managed Beans specification is quite open-ended with respect to identifying exactly which classes are managed beans. It does provide the @ManagedBean annotation as one mechanism, but it also allows other specifications to define different mechanisms. So, for example:


  • EJB规范说一个类遵循某些编程
    的限制,其中 @Stateless
    @Stateful 部署在
    EJB jar中的注释是托管bean。

  • The EJB specification says that a class obeying certain programming restrictions with a @Stateless or @Stateful annotation deployed in an EJB jar is a managed bean.

CDI规范说任何在bean部署
存档中部署了适当构造函数
的类都是托管bean。

The CDI specification says that any class with an appropriate constructor deployed in a "bean deployment archive" is a managed bean.

鉴于EJB和CDI提供
可以说更方便的方式来
识别托管bean,你可能会
确切地想知道 @ManagedBean 是什么需要
。答案,如Dan提到的
,如果您的环境中有CDI
可用(例如,如果您使用EE6,则为
),那么
@ManagedBean 只是不需要
@ManagedBean 确实有
供使用JSF2
且没有CDI
的人使用。

Given that EJB and CDI provide arguably more convenient ways to identify a managed bean, you might wonder precisely what @ManagedBean is needed for. The answer, as alluded to by Dan, is that if you have CDI available in your environment (for example, if you are using EE6), then @ManagedBean is just not really needed. @ManagedBean is really there for use by people who are using JSF2 without CDI.

OTOH,如果你注释bean
@ManagedBean ,你的环境中你有
的CDI,你仍然可以使用
CDI将东西注入你的bean。
这个
案例中的 @ManagedBean
注释不是必需

OTOH, if you do annotate a bean @ManagedBean, and you do have CDI in your environment, you can still use CDI to inject stuff into your bean. It's just that the @ManagedBean annotation is not required in this case.

总结一下,如果你有CDI
可用,它提供
优质编程模型给你
@ManagedBean / @ManagedProperty JSF2从JSF1继承的模型
。所以
优越,事实上,EE 6 web
配置文件不需要支持
@ManagedProperty 等。这个想法是
你应该只使用CDI。

To summarize, if you do have CDI available to you, it provides a far superior programming model to the @ManagedBean/@ManagedProperty model that JSF2 inherits from JSF1. So superior, in fact, that the EE 6 web profile does not require support for @ManagedProperty etc. The idea being that you should just use CDI instead.

这篇关于由于CDI / Weld中的@Named,@ ManagedBeans在JavaEE6中是否已过时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:27