Apache Tomcat 7.0.47 上的 BeanManager 无法创建资源实例

Posted

技术标签:

【中文标题】Apache Tomcat 7.0.47 上的 BeanManager 无法创建资源实例【英文标题】:BeanManager on Apache Tomcat 7.0.47 cannot create resource instance 【发布时间】:2013-11-20 02:12:15 【问题描述】:

运行于: Apache Tomcat 7.0.47 OmniFaces 1.6.3 JSF 莫哈拉 2.1.26 CDI Weld 2.1.0.Final

通过访问 JNDI“java:comp/BeanManager”,在 Apache Tomcat 7.0.47 上有一个 RuntimeException。 CDI BenManager 绑定到“java:comp/env/BeanManager”在您的 BeanManager 类中未达到的 vut。

  /**
         * 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);
                    
            
    

堆栈跟踪:添加

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-配置:

<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>

【问题讨论】:

我无法用给定的版本重现您的问题。您究竟是如何在 Tomcat 上安装 Weld 的?能否请您发布整个堆栈跟踪? org.jboss.weld.servletweld-servlet2.1.0.Final 作为 web.xml 中的侦听器:org.jboss.weld.environment.servlet.Listener 【参考方案1】:

根据堆栈跟踪,

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 资源。 &lt;resource-env-ref&gt; 不知道如何创建它。您需要在您的 webapp 的 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

【讨论】:

这可能不是异常的原因。我已经有一个定义 BeanManager Resouce 的 context.xml。 对不起,例外情况另有说明。很可能您的构建已损坏,或者文件位于错误的位置(必须位于 Web 内容的 /META-INF,而不是 Java 源代码中!),或者文件名称错误(路径和名称区分大小写)。重要的是,当我从我的沙盒 web 应用程序中删除 /META-INF/context.xml 时,我可以准确重现您的问题。 我在服务器位置 conf/catalina/localhost 的 context.xml 中添加了资源条目,并且找到了 resource-env-ref。再也没有例外了。感谢您的有益建议。 后期说明:我观察到,当您通过 Context 元素配置 Web 应用程序以使其文档库位于标准 webapps 文件夹之外时,Tomcat 似乎不想拾取 META-INF/context webapp 的.xml 了。我需要将该信息直接添加到我重写的 Context 元素(在 server.xml 中)。所以更准确地说,我需要将 BeanManager 资源放在 server.xml 内的 Context 元素中,而不是放在 webapp 的 META-INF/context.xml 中,以使其在非常特定的设置中工作。

以上是关于Apache Tomcat 7.0.47 上的 BeanManager 无法创建资源实例的主要内容,如果未能解决你的问题,请参考以下文章

Linux安装tomcat

学习Solr

Java开发时候环境变量的设置

tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()

Linux下Solr的安装和配置

安装单机版solr