Mysql 调用init方法失败;嵌套异常是javax.naming.NameNotFoundException

Mysql 调用init方法失败;嵌套异常是javax.naming.NameNotFoundException,mysql,spring,maven,tomcat,tomcat8.5,Mysql,Spring,Maven,Tomcat,Tomcat8.5,错误日志: SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appDatas

错误日志:

        SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appDatasource' defined in ServletContext resource [/WEB-INF/root-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/test] is not bound in this  Context. Unable to find [jdbc].
    at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.NameNotFoundException: Name [jdbc/test] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 21 more
My web.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>RestfulWebService</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      <!-- The definition of the Root Spring Container shared by all Servlets and Filters - Starts -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/root-context.xml</param-value>
      </context-param>
      <!-- The definition of the Root Spring Container shared by all Servlets and Filters - Ends -->  

    <!-- Creates the Spring Container shared by all Servlets and Filters - Starts -->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- User Session Handling - Starts 
      <listener>
          <listener-class>com.ericsson.utility.UserSessionListener</listener-class>
      </listener>-->
      <session-config>
            <session-timeout>45</session-timeout>
      </session-config>
    <!-- User Session Handling - Ends -->
    <!-- Spring Processes application requests -Starts -->
      <servlet>
          <servlet-name>OredooWalletServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
              /WEB-INF/EngineConfig/rest-context.xml
              </param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>OredooWalletServlet</servlet-name>
          <url-pattern>/owp/*</url-pattern>
      </servlet-mapping>
    </web-app>

RestfulWebService
index.html
上下文配置位置
/WEB-INF/root-context.xml
org.springframework.web.context.ContextLoaderListener
45
OredooWalletServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/EngineConfig/rest-context.xml
1.
OredooWalletServlet
/单程证/*
My root-context.xml

                <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

                <context:annotation-config></context:annotation-config>

             <!--  Read from Properties file 
             <bean id="reportingProperties"
                    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                    <property name="locations">
                        <list>
                            <value>classpath*:owpsimulator.properties</value>
                        </list>
                    </property>
                    <property name="ignoreUnresolvablePlaceholders" value="true"/>
            </bean>  -->
             <!-- Create DataSource Bean-Starts -->
                <bean id="appDatasource" class="org.springframework.jndi.JndiObjectFactoryBean">
                    <property name="jndiName" value="java:comp/env/jdbc/test"/>
                </bean>  
             <!-- Create DataSource Bean-Ends -->   
            <!-- Create DataSource Bean-Ends -->   
            <!-- JdbcTemplate -Starts -->
                   <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">
                     <property name="fetchSize" value="100"/>
                     <property name="dataSource" ref="appDatasource"/>
                   </bean>  
             <!-- JdbcTemplate -Ends -->
             <!-- NamedParameterJdbcTemplate -Starts -->
                   <bean id="namedParameterJdbcTemplate"  class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
                      <constructor-arg ref="jdbcTemplate"/>
                   </bean>  
             <!-- NamedParameterJdbcTemplate -ends -->

            </beans>

我用spring创建了RESTWeb服务,用jdbc模板创建了mysql。我已经添加了mysql连接器jar。我也在pom.xml中包含了mysql

在tomcat server.xml中,我添加了

但我仍然无法找到这个错误的根源


添加到tomcat的context.xml后,我没有得到异常
javax.naming.NameNotFoundException
,但我的资源映射不起作用。

你确定吗?我们将资源定义放在server.xml的
中。
Tomcat为JNDI资源提供了许多特定于Tomcat的选项,这些选项不能在web.xml中指定。其中包括closeMethod,它可以在web应用程序停止时更快地清理JNDI资源,以及singleton,它可以控制是否为每个JNDI查找创建资源的新实例。要使用这些配置选项,必须在web应用程序的元素或$CATALINA_BASE/conf/server.xml元素中指定资源。
quoting from