tomcat - Spring MVC 无法获取在 JNDI 中配置的数据源

标签 tomcat spring-mvc jndi

我在使用 Spring 3.1 查找数据源和使用 Tomcat 查找 JNDI 时遇到问题。它无法找到 HSQL 数据库的驱动程序类,即使我已经为它提供了 jar。

我已经尝试将 jar 放在我的 Maven pom.xml 中,并将 jar 复制到 ${CATALINA_HOME}/lib 目录。但似乎没有任何效果。下面给出了我的堆栈跟踪。

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'EProfileConfigureSimulator': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire method: public void com.myproject.simulator.ConfigureSimulator.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: 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.NamingException: Cannot create JDBC driver of class '' for connect URL 'jdbc:hsqldb:hsql://localhost:9001/SIMULATORDB'

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
. . . . 

我的 Tomcat 配置在 $CATALINA_HOME/conf/context.xml

<Resource name="jdbc/SimulatorDB" auth="Container"
          type="javax.sql.DataSource"
          driveClassName="org.hsqldb.jdbc.JDBCDriver"
          url="jdbc:hsqldb:hsql://localhost:9001/SIMULATORDB"
          username="SA" 
          password="" 
          initialSize="25"
          maxActive="100" 
          maxIdle="30"
          maxWait="10000" />

web.xml

<resource-ref>
      <description>HSQL Database</description>
      <res-ref-name>jdbc/SimulatorDB</res-ref-name> <!--  Must match the Server Resource name -->
      <res-type>javax.sql.DataSource</res-type>  <!--  Must match the Server type  -->
      <res-auth>Container</res-auth> <!--  Must match the server auth attribute.  -->

    </resource-ref>

applicationContext.xml

        <!-- Enable annotations -->
<context:annotation-config />

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

<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/SimulatorDB"/>
  <property name="lookupOnStartup" value="true"/>
  <property name="resourceRef" value="true" />
  <property name="expectedType" value="javax.sql.DataSource" />
  <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean> -->

我的源代码

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;

class JdbcHelper {
  private DataSource dataSource;

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

  public DataSource getDataSource() {
    return this.dataSource;
  }
}

最佳答案

使用

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

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

关于tomcat - Spring MVC 无法获取在 JNDI 中配置的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23928215/

相关文章:

java - URL中的方括号导致Tomcat异常

java - Jersey Rest 服务何时实际启动/关闭?

java - 如何使用 Spring MVC 对我的 API 进行版本控制?

java - 通过 JNDI JBoss 6 hibernate - 正确完成了吗?

grails - Grails查找安全的JNDI

java - 使用 JNDI 的 LDAP 用户密码认证

eclipse - IllegalArgumentException : The servlets named [test] and [pack. HelloWorld] 都映射到不允许的 url 模式 [/another/path]

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No bean named 'leadDAO' is defined

java - 在 Spring MVC 项目中设置 Tiles 页面的标题

java - Amazon WebServices 中 Tomcat 上 J2EE webapp 的设计注意事项