Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建名为';数据源';:调用init方法失败:javax.naming.NameNotFoundException:Name[jdbc/spring]_Spring_Spring Mvc_Jdbc_Jndi - Fatal编程技术网

创建名为';数据源';:调用init方法失败:javax.naming.NameNotFoundException:Name[jdbc/spring]

创建名为';数据源';:调用init方法失败:javax.naming.NameNotFoundException:Name[jdbc/spring],spring,spring-mvc,jdbc,jndi,Spring,Spring Mvc,Jdbc,Jndi,我正在尝试在SpringWeb应用程序中执行基本的MySQL数据库连接。所有配置都与我学习的教程一样,我得到以下控制台输出: 08-Aug-2016 21:39:32.244 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.support.DefaultListableBeanFactory.getTypeForFactoryBean Bean creation exception on no

我正在尝试在SpringWeb应用程序中执行基本的MySQL数据库连接。所有配置都与我学习的教程一样,我得到以下控制台输出:

08-Aug-2016 21:39:32.244 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.support.DefaultListableBeanFactory.getTypeForFactoryBean Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
08-Aug-2016 21:39:32.248 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.support.DefaultListableBeanFactory.getTypeForFactoryBean Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
08-Aug-2016 21:39:32.254 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.support.DefaultListableBeanFactory.getTypeForFactoryBean Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
08-Aug-2016 21:39:32.255 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productsController': Unsatisfied dependency expressed through method 'setProductService' parameter 0: Error creating bean with name 'productService': Unsatisfied dependency expressed through method 'setProductDao' parameter 0: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productService': Unsatisfied dependency expressed through method 'setProductDao' parameter 0: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
08-Aug-2016 21:39:32.257 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productsController': Unsatisfied dependency expressed through method 'setProductService' parameter 0: Error creating bean with name 'productService': Unsatisfied dependency expressed through method 'setProductDao' parameter 0: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productService': Unsatisfied dependency expressed through method 'setProductDao' parameter 0: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productDAO': Unsatisfied dependency expressed through method 'setDataSource' parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spring] is not bound in this Context. Unable to find [jdbc].
这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/drewnostalDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dispatcher-servlet.xml
        /WEB-INF/config/dao-context.xml
        /WEB-INF/config/service-context.xml
    </param-value>
</context-param>


<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

数据库连接
jdbc/drewnostalDB
javax.sql.DataSource
容器
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
/WEB-INF/config/dao-context.xml
/WEB-INF/config/service-context.xml
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/

和dao-context.xml:

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

    <context:annotation-config/>
    <context:component-scan base-package="pl.drewnostal.dao"/>

    <jee:jndi-lookup jndi-name="jdbc/spring" id="dataSource" expected-type="javax.sql.DataSource"/>

</beans>

下面是Tomcat配置文件夹中的context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
  <Resource name="jdbc/drewnostalDB" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="root" password="" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/"/>

</Context>


我正在使用Tomcat8.5.4和Java8。Spring jars版本是4.3.1,mysql connector java版本是6.0.3

context.xml
dao context.xml
中的jndi名称不应该映射到您的资源吗?资源名称是“jdbc/drewnostalDB”哇,真是个愚蠢的错误,谢谢你们的回答,它现在可以工作了;)
context.xml
dao context.xml
中的jndi名称不匹配,spring配置文件中的jndi名称“jdbc/spring”不应该映射到您的资源吗?资源名称是“jdbc/drewnostalDB”哇,真是个愚蠢的错误,谢谢你们的回答,它现在可以工作了;)