jsf - @FacesValidator 和 @FacesConverter 中的 EJB 和 CDI 注入(inject)点无法通过较新版本的 JSF 中的 OmniFaces 工作

标签 jsf cdi jsf-2.2 omnifaces weld

我正在使用,

  • 莫贾拉 2.3.0-m01
  • OmniFaces 2.0。
  • PrimeFaces 5.1 最终版
  • PrimeFaces 扩展 3.0.0
  • GlassFish 服务器 4.1

以及其他 Java EE 工件。

<小时/>

@FacesValidator 中的注入(inject)点如下所示,

@FacesValidator(value="testValidator")
public class TestValidator implements Validator {

    @Inject
    private DemoEJB ejb;

    @Inject
    private ManagedBean managedBean;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // Use the injected EJB and/or managed bean here.
    }
}

这些注入(inject)点仍然为null(我没有明确尝试注入(inject) EJB,但它不应该像托管 bean 那样发生)。

<小时/>

由于上述 Mojarra 版本仍然可以作为快照使用,因此我尝试使用相同版本的 OmniFaces 2.0 将其降级到 2.2.10(或者也尝试使用 OmniFaces 2.1-SNAPSHOT - 在两个 Mojarra 2.2.10 上)和 2.3.0-m01 或者)但无济于事。

当我将 Majarra 降级到 2.2.8-02 时(交替尝试使用 OmniFaces 1.8.1、2.0 和 2.1-SNAPSHOT),此方法有效。我没有尝试其他 Mojarra 版本。

支持通过 OmniFaces 为 EJB 和 CDI 注入(inject)点制作 @FacesValidator@FacesConverter 候选对象(这又不需要任何额外的依赖项和/或配置)已被新版本的 Mojarra 删除?

我还没有在 @FacesConverter 中明确尝试过,只是因为使用有缺陷的 NetBeans IDE 多次更改了这么多库,而 NetBeans IDE 本身在 Windows 上运行速度也很慢,需要一整天的时间从黎明到黄昏。

<小时/>

编辑:

服务器产生以下与 Weld 相关的警告。

WARN:   WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
WARN:   WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.ApplicationProducer@b15a70 declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.ApplicationMapProducer@db0450 declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.ViewMapProducer@1c55365 declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.ExternalContextProducer@14b1a6 declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.FacesContextProducer@1048acb declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.
WARN:   WELD-001473: javax.enterprise.inject.spi.Bean implementation com.sun.faces.cdi.ViewProducer@275cfa declared a normal scope but does not implement javax.enterprise.inject.spi.PassivationCapable. It won't be possible to inject this bean into a bean with a passivating scope (@SessionScoped, @ConversationScoped). This can be fixed by assigning the Bean implementation a unique id by implementing the PassivationCapable interface.

最佳答案

这是由 Mojarra 2.2.9 中的更改引起的。在 2.2.8 中运行良好。我在 GlassFish 4.1 和 WildFly 8.2 中重现了它。扫描 2.2.9 release notes 中列出的所有问题后以及相关的更改包,这似乎是 issue 3552 向后移植的结果到 JSF 2.2.x。他们在内部禁用了组件、行为、验证器和转换器的可注入(inject)性检查,这隐式地将它们注册为 CDI 托管 bean 候选者。换句话说,在 2.2.x 到 2.2.9 期间,Mojarra 无意中在上述工件中提供了“原生”@Inject 支持。

为了让 @FacesConverter@FacesValidator 中的注入(inject)通过 OmniFaces 正常工作,您需要添加一个空的 /WEB-INF/beans。 xml 或至少一个将 bean-discovery-mode 设置为 all 而不是 annotated 的文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:weld="http://jboss.org/schema/weld/beans"
    bean-discovery-mode="all"
>
    <!-- ... -->
</beans>

它已经是默认值,您也可以省略 bean-discovery-mode 属性。

技术差异在于,bean-discovery-mode="all" 会将所有 符合条件的类注册为 CDI 托管 bean,而 bean-discovery-mode ="annotated" 仅将具有显式 CDI 范围注释(例如 @RequestScoped)的类注册为 CDI 托管 bean,因此对于 @FacesConverter 来说,这会失败>@FacesValidator

关于jsf - @FacesValidator 和 @FacesConverter 中的 EJB 和 CDI 注入(inject)点无法通过较新版本的 JSF 中的 OmniFaces 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29458023/

相关文章:

java - CDI GenericDAO 和自定义实现

java - 如何使用 AS6 在 JMX 中使用 CDI 获取 'JBoss service'?

jsf - f :param in JSF的条件渲染

ajax - 第一次单击命令按钮不会重定向到另一个页面

database - 从数据库加载 Facelets 模板

jsf - 如何创建动态 JSF 表单域

java - JSF EL : Fallback if l18n-Propperty not found

java - 如何注入(inject)实现相同接口(interface)的两个不同类的两个实例?

java - JSF - RichFaces 显示复杂页面的性能问题

JSF、RichFaces、分页