Spring Boot:无法加载驱动程序类:org.hsqldb.jdbcDriver

Mar*_*cel 5 spring spring-boot

我有一个简单的Spring Boot应用程序(通过Spring Roo生成)。

数据库配置如下:

spring.datasource.driver-class-name=org.hsqldb.jdbcDriver
spring.datasource.url=jdbc\:hsqldb\:mem\:PetClinic
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.messages.encoding=ISO-8859-1
spring.messages.fallback-to-system-locale=false
spring.thymeleaf.mode=html
Run Code Online (Sandbox Code Playgroud)

这是我如何声明HSQLDB依赖关系:

<dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

启动应用程序时,出现错误:

Caused by: java.lang.IllegalStateException: Cannot load driver class: org.hsqldb.jdbcDriver
    at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:214) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
Run Code Online (Sandbox Code Playgroud)

Spring-boot-autoconfigure模块尝试使用ClassUtils加载当前上下文类的实用程序类来加载该类。

我想知道这种方法是否工作正常,因为我使用了负责加载Maven依赖项的Tomcat容器?为什么即使使用libs目录中的JAR,Spring也无法找到它?

小智 6

我看到您提供的范围已提供,<scope>provided</scope>我不认为 Tomcat 提供了开箱即用的 hsqldb.jar。

因此,请尝试删除提供的范围。


Str*_*lok 5

  1. <scope>provided</scope>从您的中删除pom.xml
  2. 从应用程序属性中删除spring.datasource.driver-class-namespring.datasource.url属性

因为:

  • spring.datasource.url提供的驱动程序类名称是多余的时,Spring Boot会自动尝试加载正确的驱动程序。
  • 因为您要使用嵌入式数据库,所以根本不需要提供spring.datasource.url。只需在类路径上具有嵌入式数据库JAR(例如HSQLDB)

相关文档摘要:

Spring Boot可以自动配置嵌入式H2,HSQL和Derby数据库。您无需提供任何连接URL,只需包含要使用的嵌入式数据库的构建依赖项即可。

请阅读Spring Boot文档中的“ 使用SQL数据库”部分。我所说的一切都在这里提到,因此您可以获取更多详细信息。