2

我使用的是 Tomcat 8 (8.0.30)、OpenWebBeans 1.6.2、JSF Mojarra 2.2.12 和 OmniFaces 1.8。我决定将 OmniFaces 升级到更高版本。在 2.0 之后,它似乎需要 CDI,但是在启动 Tomcat(使用 OmniFaces 2.2)时,出现以下错误。根据 OmniFaces 的兼容性矩阵,Tomcat+Mojarra+OWB 应该适用于 8.0.30+2.2.12+1.6.2。

我也尝试过 2.0,但同样的错误。在 2.1 中我遇到了另一个问题,但我只是假设它在 2.2 中是固定的,因为那里的行为与 2.0 中的相同。我对 2.3-RC1 也有同样的问题。

是否需要做一些具体的事情才能让 OmniFaces 识别我正在运行 CDI?这可能是 OmniFaces 中的错误吗?

我创建了一个非常小的示例应用程序来复制这个问题,所以我确信我们的主应用程序中没有其他任何干扰。

mar 22, 2016 10:57:46 FM org.apache.webbeans.lifecycle.AbstractLifeCycle bootstrapApplication
INFO: OpenWebBeans Container has started, it took [447] ms.
mar 22, 2016 10:57:46 FM org.omnifaces.ApplicationListener checkCDIAvailable
SEVERE: 
????????????????????????????????????????????????????????????????????????????????
?                         ??     ?                                             ?
?    ?                  ?????    ? OmniFaces failed to initialize!             ?
?   ????               ??????    ?                                             ?
?   ??????            ???????    ? This OmniFaces version requires CDI, but    ?
?   ???????          ???????     ? none was found on this environment.         ?
?    ???????????????????????     ?                                             ?
?    ???????????????????????     ? OmniFaces 2.x requires a minimum of JSF 2.2.?
?     ?????????????????????      ? Since this JSF version, the JSF managed bean?
?     ?????????????????????      ? facility @ManagedBean is semi-official      ?
?     ??????????????????????     ? deprecated in favour of CDI. JSF 2.2 users  ?
?     ??????????????????????     ? are strongly encouraged to move to CDI.     ?
?     ??????????????????????     ?                                             ?
?    ???????????????????????     ? OmniFaces goes a step further by making CDI ?
?    ????????????????????????    ? a REQUIRED dependency next to JSF 2.2. This ?
?    ????????????????????????    ? not only ensures that your web application  ?
?   ???????????????? ????????    ? represents the state of art, but this also  ?
?   ?????????????????????????    ? makes for us easier to develop OmniFaces,   ?
?   ??????????????????????????   ? without the need for all sorts of hacks in  ?
?   ??????????????????????????   ? in order to get OmniFaces to deploy on      ?
?  ????????????????????????????  ? environments without CDI.                   ?
?  ????????????????????????????  ?                                             ?
?  ????????????????????????????  ? You have 3 options:                         ?
?????????????????????????????????? 1. Downgrade to OmniFaces 1.x.              ?
?????????????????????????????????? 2. Install CDI in this environment.         ?
?????????????????????????????????? 3. Switch to a CDI capable environment.     ?
??????????????????????????????????                                             ?
?????????????????????????????????? For additional instructions, check          ?
?????????????????????????????????? http://omnifaces.org/cdi                    ?
??????????????????????????????????                                             ?
????????????????????????????????????????????????????????????????????????????????
mar 22, 2016 10:57:46 FM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.omnifaces.ApplicationListener
java.lang.ExceptionInInitializerError
    at org.omnifaces.ApplicationListener.checkCDIAvailable(ApplicationListener.java:74)
    at org.omnifaces.ApplicationListener.contextInitialized(ApplicationListener.java:58)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: CDI BeanManager instance is not available in JNDI.
    at org.omnifaces.config.BeanManager.<init>(BeanManager.java:99)
    at org.omnifaces.config.BeanManager.<clinit>(BeanManager.java:49)
    ... 11 more
Caused by: java.lang.IllegalStateException: javax.naming.NamingException: Cannot create resource instance
    at org.omnifaces.util.JNDI.lookup(JNDI.java:95)
    at org.omnifaces.config.BeanManager.<init>(BeanManager.java:95)
    ... 12 more
Caused by: javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.omnifaces.util.JNDI.lookup(JNDI.java:90)
    ... 13 more

这是我正在使用的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>se.app.testapp</groupId>
    <artifactId>sampleapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-impl</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-jsf</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-el22</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-web</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <type>jar</type>
            <classifier>sources</classifier>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <type>jar</type>
        </dependency>

    </dependencies>
</project>
4

1 回答 1

0

您的异常原因是:

java.lang.IllegalStateException:CDI BeanManager 实例在 JNDI 中不可用。

换句话说,在 OmniFaces 的上下文初始化期间,CDI BeanManager 没有在 JNDI 中注册。通常,Tomcat 具有只读 JNDI,任何新的 JNDI 资源都必须通过 webapp 的/META-INF/context.xml. 对于 Weld,这是详细的How to install CDI in Tomcat? 对于 OpenWebBeans,您需要以下一个:

<Context>
    <Resource name="BeanManager"
        auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
        factory="org.apache.webbeans.container.ManagerObjectFactory" />
</Context>

此外,对于 OWB,您还需要在 webapp 中显式注册其上下文侦听器,/WEB-INF/web.xml以确保它在 OmniFaces 之前被初始化。

<listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>

@Eager否则,当 OmniFaces开始初始化应用程序范围的 bean时,您可能会遇到如下所示的异常:

java.lang.IllegalStateException:不允许在 AfterBeanDiscovery 之前调用 getBeans(Type, Annotation...)

尽管您已经指定了 OmniFaces 2.2,但我想为未来的读者添加一个注释,即至少需要 OmniFaces 2.1 才能与手动安装在 Tomcat 上的 OWB 和平合作(OmniFaces 2.0 在 TomEE 上运行良好),另请参阅问题 133@Inject修复了 2 个主要问题,即初始化排序和@WebListener.


更新:“如何在 Tomcat 中安装 CDI? ”文章刚刚更新了 OpenWebBeans 的说明。

于 2016-03-22T11:33:31.627 回答