将JSF托管Bean迁移到CDI托管Bean [英] Migrate JSF managed beans to CDI managed beans

查看:111
本文介绍了将JSF托管Bean迁移到CDI托管Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将Web应用程序从使用JSF托管Bean转换为使用CDI托管Bean.我知道我需要执行以下操作:

I'm planning to convert a web app from using JSF managed bean to using CDI managed beans. I know I'll need to do below:

  1. 在WEB-INF中添加一个空的beans.xml文件.
  2. 将所有JSF @ManagedBean替换为CDI @Named注释.
  3. 用CDI或OmniFaces范围注释替换所有JSF范围注释.
  4. 用CDI @Inject注释替换所有JSF @ManagedProperty.

这是需要做的所有事情吗?我有什么需要注意的陷阱吗?

Is that all that needs to be done? Are there any gotchas that I need to be aware of?

推荐答案

基本上,只要您已经在Java EE服务器上,这就是您要做的所有事情.在Tomcat上时,您需要首先手动安装CDI.有关Weld和OpenWebBeans的说明,请参见博客如何在Tomcat中安装CDI?

Basically, that's indeed all you need to do provided that you're on a Java EE server already. When on Tomcat, you'd need to manually install CDI first. Instructions for both Weld and OpenWebBeans are detailed in the blog How to install CDI in Tomcat?

以下陷阱必须得到照顾:

The below gotchas need to be taken care of:

  • 虽然OmniFaces 2.x正式"需要JSF 2.2,但OmniFaces 2.0/2.1在技术上与JSF 2.1向后兼容,并且在TomEE的情况下也应在具有JSF 2.1的TomEE 1.x上工作,但是OmniFaces 2.2具有硬的JSF 2.2依赖关系(由于新的<o:viewAction>标记),并且如果不将其MyFaces JSF实现升级到2.2兼容版本,或者将自身升级到TomEE 7.x,就不会部署在TomEE 1.x上.另请参见 OmniFaces兼容性矩阵.

  • While OmniFaces 2.x "officially" requires JSF 2.2, OmniFaces 2.0/2.1 is technically backwards compatible with JSF 2.1 and should in TomEE's case work on TomEE 1.x with JSF 2.1 as well, but OmniFaces 2.2 has a hard JSF 2.2 dependency (due to the new <o:viewAction> tag) and won't deploy on TomEE 1.x without upgrading its MyFaces JSF implementation to a 2.2 compatible version, or itself being upgraded to TomEE 7.x. See also OmniFaces Compatibility Matrix.

当您部署一个带有多个WAR且各自拥有自己的OmniFaces库的EAR时,通常所有CDI功能都只能在一个WAR中工作,因为WAR提供的库的CDI上下文被错误地解释为EAR范围.这是CDI规范中的一个疏漏,并且在将来的CDI版本中将得到修复.另请参见 OmniFaces已知问题(CDI).

When you deploy an EAR with multiple WARs with each its own OmniFaces library, then generally all CDI functionality will work in only one WAR as the CDI context of a WAR-provided library is incorrectly interpreted as EAR-wide. This is an oversight in CDI spec and yet to be fixed in a future CDI version. See also OmniFaces Known Issues (CDI).

当您想在@FacesConverter@FacesValidator中使用OmniFaces提供的CDI注入支持时,您将创建/使用与CDI 1.1兼容的beans.xml(因此与CDI 1.0兼容)一个或一个空的),则需要确保已在beans.xml中显式设置了bean-discovery-mode="all".另请参见 @FacesConverter展示柜.

When you want to use OmniFaces-provided CDI injection support in @FacesConverter or @FacesValidator, and you're going to create/use a CDI 1.1 compatible beans.xml (and thus not a CDI 1.0 compatible one or an empty one), then you need to make sure that you have explicitly set bean-discovery-mode="all" in beans.xml. See also @FacesConverter showcase.

替换@ManagedBean(eager=true)时,请注意,标准CDI对此没有直接等效项.您将为此使用@Observes. OmniFaces为此提供了 @Eager 批注.另请参见如何配置启动受管bean?

When replacing @ManagedBean(eager=true), be aware that standard CDI has no direct equivalent for this. You would use @Observes for this. OmniFaces offers @Eager annotation for the purpose. See also How to configure a start up managed bean?

在JSF 2.0/2.1/2.2中替换@ManagedProperty时,请注意,您不能仅通过@Inject直接注入#{param.xxx}#{cookie.xxx}#{initParam.xxx},但这是可能的通过@ManagedProperty. OmniFaces分别提供 @Param @ContextParam .仅在JSF 2.3中存在一个新的@javax.faces.annotation.ManagedProperty批注,该批注的使用方式与从JSF 2.3开始不推荐使用的原始@javax.faces.bean.ManagedProperty完全相同.

When replacing @ManagedProperty in JSF 2.0/2.1/2.2, be aware that you can't inject #{param.xxx}, #{cookie.xxx} and #{initParam.xxx} directly via @Inject alone, while that was just possible via @ManagedProperty. OmniFaces offers respectively @Param, @Cookie and @ContextParam for the purpose. Only in JSF 2.3 there's a new @javax.faces.annotation.ManagedProperty annotation which can be used exactly the same way as original @javax.faces.bean.ManagedProperty which got deprecated since JSF 2.3.

这篇关于将JSF托管Bean迁移到CDI托管Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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