转换器中的注入在 JSF 2.3 中不起作用

Injection in a converter does not work in JSF 2.3

服务器:Payara 5.183.

使用转换器时,会引发 NullPointerException,因为注入的 EJB 为空(System.out.println 打印 "null")。

如果我使用在 JSF 2.3 之前使用的解决方法:用 @Name 替换 @FacesConverter,它会起作用(注入不为空)。

转换器:

@FacesConverter(value = "compteConverter", managed = true)
public class CompteConverter implements Converter<CompteBancaire> {

  @EJB
  private GestionnaireCompte gestionnaireCompte;

  @Override
  public CompteBancaire getAsObject(FacesContext context, UIComponent component, String id) {
    if (id == null || id.isEmpty()) {
      return null;
    }
    try {
      System.out.println("*****EJB gestionnaireCompte=" + gestionnaireCompte);
      return gestionnaireCompte.getCompte(Long.parseLong(id));
    } catch (NumberFormatException e) {
      throw new ConverterException(new FacesMessage("Id de compte invalide"), e);
    }
  }

  @Override
  public String getAsString(FacesContext arg0, UIComponent arg1, CompteBancaire compte) { ... }

这个转换器的用法:

  <ui:define name="metadata">
    <f:metadata>

      <f:viewParam name="id" value="#{operations.compte}"
                     converter="compteConverter"/>

是否是 Mojarra/Payara 的错误(managed = true 不工作)或者你能帮我找到我的错误吗?

默认情况下托管转换器不工作。为了使它们工作,我添加了一个由 @FacesConfig 注释的 CDI bean(对于要使用的 JSF 2.3)和 @ApplicationScoped(它将是一个带有此注释的 CDI bean)。