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

查看:24
本文介绍了将 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?

推荐答案

基本上,只要您已经在 J​​ava 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?

需要注意以下问题:

  • 虽然 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> 标记)并且不会在 TomEE 1.x 上部署,除非将其 MyFaces JSF 实现升级到 2.2 兼容版本,或者本身是升级到 TomEE 7.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 且每个 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 兼容的或空的),那么您需要确保已明确设置 bean-discovery-mode="all"beans.xml 中.另请参阅 @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 时,请注意不能注入 #{param.xxx}#{cookie.xxx}#{initParam.xxx} 直接通过 @Inject 单独,而这只能通过 @ManagedProperty.OmniFaces 分别提供 @Param, @Cookie@ContextParam 用于此目的.只有在 JSF 2.3 中有一个新的 @javax.faces.annotation.ManagedProperty 注释,它的使用方式与原来的 @javax.faces.bean.ManagedProperty 完全相同自 JSF 2.3 起已弃用.

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天全站免登陆