【问题标题】:Cannot load driver class: org.h2.Driver in spring boot无法加载驱动程序类: org.h2.Driver in spring boot
【发布时间】:2020-12-08 18:05:02
【问题描述】:

我正在尝试使用 h2 数据库和微服务。但是当我运行它没有运行的应用程序时,它显示错误。 我有尤里卡服务器和产品微服务。在产品微服务中,我使用了 h2database。它显示错误。

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception 
     is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'h2Console' defined in class path resource 
[org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.class]: Bean instantiation via 
factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 
'h2Console' threw exception; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' 
defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean 
 instantiation via factory method failed; nested exception is 
 org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'h2Console' threw 
exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'dataSource' defined in class path resource 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'dataSource' defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean 
instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception 
is java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver 

Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver  

以下是我在 pom.xml 中的依赖项

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

下面是我的Product微服务的application.properties文件

server.port=8082

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.datasource.name=SunHome

#H2 settings
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.datasource.url=jdbc:h2:mem:testdb  
spring.datasource.driverClassName=org.h2.Driver  
spring.datasource.username=sa  
springspring.datasource.password=  
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect  

请帮我解决这个错误。

【问题讨论】:

    标签: spring-boot microservices h2


    【解决方案1】:

    检查 applcation.properties 是否有“奇怪”字符 配置应该是对的。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2019-06-11
      • 2021-12-15
      • 1970-01-01
      • 2017-11-29
      • 2018-08-04
      • 2018-11-13
      • 2018-01-31
      相关资源
      最近更新 更多