运行:
Apache Tomcat 7.0.47
OmniFaces 1.6.3
JSF Mojarra 2.1.26
CDI焊接2.1.0。最终版

Threre是通过访问JNDI“ java:comp / BeanManager”在Apache Tomcat 7.0.47上的RuntimeException。 CDI BenManager绑定到BeanManager类中未达到的“ java:comp/env/BeanManager”值。

  /**
         * Perform automatic initialization whereby the bean manager is looked up from the JNDI. If the bean manager is
         * found, then invoke {@link #init(Object)} with the found bean manager.
         */
        private void init() {
                if (!initialized.getAndSet(true)) {
                        try {
                                Class.forName("javax.enterprise.inject.spi.BeanManager"); // Is CDI present?
                                Class.forName("javax.naming.InitialContext"); // Is JNDI present? (not on Google App Engine)
                        }
                        catch (Exception e) {
                                return; // CDI or JNDI not supported on this environment.
                        }
                    try {
                            Object beanManager = JNDI.lookup("java:comp/BeanManager"); // CDI spec.

                            if (beanManager == null) {
                                    beanManager = JNDI.lookup("java:comp/env/BeanManager"); // Tomcat.
                            }

                            if (beanManager == null) {
                                    return; // CDI not registered on this environment.
                            }

                            this.beanManager = beanManager;
                            Class<?> beanManagerClass = beanManager.getClass();
                            Class<?> contextualClass = Class.forName("javax.enterprise.context.spi.Contextual");
                            Class<?> beanClass = Class.forName("javax.enterprise.inject.spi.Bean");
                            Class<?> creationalContextClass = Class.forName("javax.enterprise.context.spi.CreationalContext");
                            getBeans = beanManagerClass.getMethod("getBeans", Type.class, Annotation[].class);
                            resolve = beanManagerClass.getMethod("resolve", Set.class);
                            createCreationalContext = beanManagerClass.getMethod("createCreationalContext", contextualClass);
                            getReference = beanManagerClass.getMethod("getReference", beanClass, Type.class, creationalContextClass);
                    }
                    catch (Exception e) {
                            logger.log(Level.SEVERE, LOG_INITIALIZATION_ERROR, e);
                            throw new RuntimeException(e);
                    }
            }
    }


Stacktrace:已添加

Nov 08, 2013 12:10:01 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: .
Nov 08, 2013 12:10:01 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 08, 2013 12:10:01 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 08, 2013 12:10:01 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 607 ms
Nov 08, 2013 12:10:01 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 08, 2013 12:10:01 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Nov 08, 2013 12:10:01 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor E:\Tomcats\apache-tomcat-7.0.47\conf\Catalina\localhost\
Nov 08, 2013 12:10:09 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Mojarra 2.1.26 ( 20130905-1451 https://svn.java.net/svn/mojarra~svn/tags/2.1.26@12478) für Kontext '/a' wird initialisiert.
Nov 08, 2013 12:10:09 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy-Annotationen vorhanden.  Verwaltete Bean-Methoden, die mit diesen Annotationen markiert sind, lassen die entsprechenden Annotationen verarbeiten.
Nov 08, 2013 12:10:10 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
Nov 08, 2013 12:10:10 PM org.omnifaces.VersionLoggerEventListener processEvent
INFO: Using OmniFaces version 1.6.3
Nov 08, 2013 12:10:10 PM com.sun.faces.config.ConfigureListener$WebConfigResourceMonitor$Monitor <init>
INFO: Monitoring jndi:/localhost/../WEB-INF/faces-config.xml for modifications
Nov 08, 2013 12:10:10 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory E:\Tomcats\apache-tomcat-7.0.47\webapps\host-manager
Nov 08, 2013 12:10:11 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory E:\Tomcats\apache-tomcat-7.0.47\webapps\manager
Nov 08, 2013 12:10:11 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory E:\Tomcats\apache-tomcat-7.0.47\webapps\ROOT
Nov 08, 2013 12:10:11 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 08, 2013 12:10:11 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 08, 2013 12:10:11 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9572 ms
Nov 08, 2013 12:10:32 PM org.omnifaces.config.BeanManager init
SEVERE: BeanManager enum singleton failed to initialize.
java.lang.IllegalStateException: javax.naming.NamingException: Cannot create resource instance
    at org.omnifaces.util.JNDI.lookup(JNDI.java:87)
    at org.omnifaces.config.BeanManager.init(BeanManager.java:79)
    at org.omnifaces.config.BeanManager.getReference(BeanManager.java:115)
    at org.omnifaces.application.OmniApplication.createValidator(OmniApplication.java:105)
    at org.jboss.weld.environment.servlet.jsf.ForwardingApplication.createValidator(ForwardingApplication.java:152)
    at com.sun.faces.component.validator.ComponentValidators.addValidatorsToComponent(ComponentValidators.java:280)
    at com.sun.faces.component.validator.ComponentValidators.addDefaultValidatorsToComponent(ComponentValidators.java:147)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.processValidators(ComponentTagHandlerDelegateImpl.java:541)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.privateOnComponentPopulated(ComponentTagHandlerDelegateImpl.java:522)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:194)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at com.sun.faces.facelets.tag.jsf.core.FacetHandler.apply(FacetHandler.java:104)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:166)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:320)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:379)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:358)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
    at com.sun.faces.facelets.tag.ui.IncludeHandler.apply(IncludeHandler.java:120)
    at com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:106)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:178)
    at com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(DefaultFaceletContext.java:395)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:366)
    at com.sun.faces.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:111)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:188)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:320)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:379)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:358)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:155)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:164)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:914)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.omnifaces.filter.FacesExceptionFilter.doFilter(FacesExceptionFilter.java:56)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.omnifaces.filter.GzipResponseFilter.doFilter(GzipResponseFilter.java:149)
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
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(NamingManager.java:321)
    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(InitialContext.java:411)
    at org.omnifaces.util.JNDI.lookup(JNDI.java:83)
    ... 92 more


Faces-Config:

<factory>
    <exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
</factory>


web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">


  <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>

  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/taglib.xml;/WEB-INF/functions.taglib.xml</param-value>
  </context-param>

  <filter>
    <filter-name>facesExceptionFilter</filter-name>
    <filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>facesExceptionFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
  <listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  </listener>

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/WEB-INF/errorpages/expired.xhtml</location>
  </error-page>
  <error-page>
    <exception-type>com.sun.faces.context.FacesFileNotFoundException</exception-type>
    <location>/WEB-INF/errorpages/expired.xhtml</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/errorpages/500.xhtml</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/errorpages/404.xhtml</location>
  </error-page>
  <filter>
    <filter-name>gzipResponseFilter</filter-name>
    <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
  </filter>
        <filter-mapping>
                <filter-name>gzipResponseFilter</filter-name>
                <url-pattern>*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>ERROR</dispatcher>
        </filter-mapping>


  <resource-env-ref>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  </resource-env-ref>

<session-config><tracking-mode>COOKIE</tracking-mode></session-config>
</web-app>


在META-INF下添加了context.xml:

<Context>
   <Resource name="BeanManager"
      auth="Container"
      type="javax.enterprise.inject.spi.BeanManager"
      factory="org.jboss.weld.resources.ManagerObjectFactory"/>
    <Manager pathname="" />
</Context>

最佳答案

根据堆栈跟踪,

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(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    ...


您忘记在JNDI中注册BeanManager资源。 <resource-env-ref>不知道如何创建它。您需要在Web应用程序的Web内容中创建具有以下内容的/META-INF/context.xml,以便在JNDI中注册BeanManager工厂,以便JNDI知道如何创建它。

<Context>
    <Resource name="BeanManager"
        auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
        factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>


也可以看看:


How to install CDI in Tomcat?
Weld documentation

08-05 08:53