java - JNDI 与 spring mvc3 集成

标签 java tomcat spring-mvc jdbc jndi

我正在使用 Spring MVC 3 和 MySQL 服务器。我正在尝试将 JNDI 用于 JDBC 连接,但它返回 NULL DataSource。这是抛出空指针异常的一段代码。

server.xml 文件包含:

  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
  </GlobalNamingResources>

context.xml文件内容

    <Resource name="jdbc/Test" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/test"/>

web.xml 文件包含:

     <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/Test</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>

despatcher-servlet.xml 文件:

    <bean name="myDataSourceInJndi" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:comp/env/jdbc/Test</value>
        </property>
    </bean>

    <bean name="dbConfiguration" class="com.biztree.springtest.database.DataBaseConfiguration" >
        <property name="dataSource" ref="myDataSourceInJndi" />
    </bean>

DataBaseConfiguration.java

package com.biztree.springtest.database;

import javax.sql.DataSource;

public class DataBaseConfiguration {

    DataSource dataSource;

    public DataBaseConfiguration() {
        // TODO Auto-generated constructor stub
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DataSource getDataSource() {
        return dataSource;
    }
}

听到的是连接的代码

          /*    this code work throw NullPointerException */
        try {

            DataBaseConfiguration baseConfiguration = new DataBaseConfiguration();
            DataSource ds = baseConfiguration.getDataSource();
            System.out.println("ds Object : " + ds);
            connection = ds.getConnection();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

但是 ds 是空的。

如果我使用下面的代码比它正常工作

          /*    this code work fine */
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)  envCtx.lookup("jdbc/Test");

            System.out.println("ds Object : " + ds);
            connection = ds.getConnection();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

最佳答案

您需要打开 spring 的调试日志记录,和/或调试 spring 以查看它实际查找的内容,并将其与您的直接 JNDI 代码正在执行的操作进行比较。

像这样的事情也可能让你失望:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jndi/JndiLocatorSupport.html#setResourceRef(boolean )

(您在 JndiObjectFactoryBean 上设置此属性,它会自动添加 comp/env 部分...检查默认值是什么并确保其设置正确)

无论如何,一旦你调试 spring 以查看它在做什么,你就能够确认。

关于java - JNDI 与 spring mvc3 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10331391/

相关文章:

java - 无法在我的 Servlet 程序中重定向到外部 URL(如 google.com)

java - 限制ip地址访问web服务

javascript - 如何在没有 CORS 问题的情况下从外部域获取 ttf

java - Spring 验证请求正文作为字符串

spring - Spring MVC REST 的文件上传问题

java - 为什么JAVA WindowsLookAndFeel UI在Win7和Win2003中看起来不同?

java - JTable 单元格编辑器仅接受数字

java - 如何在其他类中使用 JColorChooser 给定颜色?

java - Gradle 包装器版本问题

java - 使用不同用户名登录时获取 "Maximum sessions of 1 for this principal exceeded"