javax.naming.NameNotFoundException:名称[jdbc / spitterDS]未绑定在此Context中。找不到[jdbc]

时间:2014-02-21 12:51:28

标签: java mysql spring-mvc jdbc jndi

我正在尝试使用Spring的datasource标记访问tomcat中的JNDI jee jndi-lookup

异常表明我没有正确注册datasource,但我无法弄清楚原因。

这是我的代码: -

服务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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    http://www.springframework.org/schema/jee 
                    http://www.springframework.org/schema/jee/spring-jee.xsd">

    <context:component-scan base-package="spitter" />
    <mvc:annotation-driven />

    <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />

</beans>

web应用/ META-INF / context.xml中 的: -

<Context path="/spitter" reloadable="true" cachingAllowed="false" antiResourceLocking="true">

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<Resource name="jdbc/spitterDS" auth="Container" type="org.apache.commons.dbcp.BasicDataSource"
    maxActive="100" maxIdle="30" maxWait="10000" username="root" password="password"
    driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/spitter" />
</Context>

的web.xml 的: -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Spitter</display-name>

<resource-ref>
    <description>Spitter DS</description>
    <res-ref-name>jdbc/spitterDS</res-ref-name>
    <res-type>org.apache.commons.dbcp.BasicDataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:service-context.xml
    </param-value>
</context-param>

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

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

</web-app>

最后,

MainController.java 的: -

package spitter.mvc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/")
public class MainController {
    @Resource(name="dataSource")
    private DataSource dataSource;

    @RequestMapping(method = RequestMethod.GET)
    public String showHome(Model model) throws SQLException{
        Connection con = dataSource.getConnection();

        return "spitter";
    }
}

我在Tomcat 7中为JEE开发人员使用Eclipse kepler。我在eclipse中启动tomcat服务器时的异常: -

  

org.springframework.beans.factory.BeanCreationException:创建名为'mainController'的bean时出错:注入资源   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为'dataSource'的bean:调用init方法   失败;嵌套异常是javax.naming.NameNotFoundException:Name   [jdbc / spitterDS]未绑定在此Context中。无法找到[jdbc]。

我认为我在某个地方弄错了但却找不到它。请帮忙!

4 个答案:

答案 0 :(得分:3)

"/jdbc/spitterDS"不等于"jdbc/spitterDS"

service-context.xml档案中,
改变

<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/spitterDS" resource-ref="true" />

修改

  

创建名为'dataSource'的bean时出错:

根据文件:

  

...在@Resource(name="jdbc/Foo") DataSource ds;注释中,   全局JNDI名称为jdbc/Foo   ...
  应用程序代码中的@Resource注释如下所示:
  @Resource(name="jdbc/helloDbDs") javax.sql.DataSource ds;

更改:

@Resource(name="dataSource")
private DataSource dataSource;

致:

@Resource(name="jdbc/spitterDS")
private DataSource dataSource;

请参阅 Using the Java Naming and Directory Interface

答案 1 :(得分:2)

我也遇到了这个问题,在我的情况下,我错过了tomcat日志中的WARN,我没有包含jar

org.apache.commons.dbcp.BasicDataSourceFactory 

commons-dbcp-x.x.jar
commons-pool-x.x.x.jar
commons-lang-x.x.x.jar

修正了这一切,一切正常。

答案 2 :(得分:1)

在我的情况下,我不得不增加my.cnf中的设置 max_connections = 120 我得到的印象是,任何对mysql的错误配置都会给出此错误消息。

答案 3 :(得分:0)

我得到了同样的错误。但是对于我来说,资源

中缺少了
driverClassName="oracle.jdbc.OracleDriver"

添加之后,一切都运转良好。