【问题标题】:Spring MVC unable to pick up Datasource configured in JNDISpring MVC 无法获取在 JNDI 中配置的数据源
【发布时间】:2014-05-29 07:37:36
【问题描述】:

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

我尝试将 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)
. . . . 

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

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

【问题讨论】:

    标签: tomcat spring-mvc jndi


    【解决方案1】:

    使用

    &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/SimulatorDB" expected-type="javax.sql.DataSource" /&gt;

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

    【讨论】:

    • 我应该为 JndiObjectFactoryBean 使用不同的名称还是在使用 jee:jndi 绑定时使用不同的名称?
    • 我终于找到了错误的原因。我没有在我的 tomcat 配置中使用“driverClassName”属性,而是“driveClassName”(缺少 R)。这导致应用程序崩溃。
    【解决方案2】:

    (代表问题作者发布).

    问题是因为我拼错了driverClassName这个词。我在没有“r”的情况下包含了它。应用程序现已成功部署。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多