繁体   English   中英

带有Spring MVC3集成的JNDI

[英]JNDI with spring mvc3 integration

我正在使用Spring MVC 3和MySQL服务器。 我试图将JNDI用于JDBC连接,但是它返回NULL数据源。 这是引发空指针异常的代码段。

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的调试日志记录,和/或debug spring来查看其实际查找内容,并将其与您的直接JNDI代码所执行的操作进行比较。

这样的事情也可能使您失望:

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

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

无论如何,一旦调试spring,您就可以确认它在做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM