Spring MVC无法获取在JNDI中配置的Datasource

时间:2014-05-29 07:37:36

标签: tomcat spring-mvc jndi

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

2 个答案:

答案 0 :(得分:0)

使用

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

答案 1 :(得分:0)

(代表作者提问)

问题是因为我拼错了 driverClassName 这个词。我把它包括在没有“r”的地方。该应用程序现已成功部署。