简体   繁体   English

如何解决无法在 spring-boot 应用程序中加载驱动程序 class: org.h2.Driver 的问题?

[英]How to resolve unable to load driver class: org.h2.Driver in spring-boot application?

I'm facing this error:我面临这个错误:

Error creating bean with name 
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0;      
 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

Why I'm facing this issue?为什么我会遇到这个问题?

Here is the pom.xml:这是 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wipro.boot</groupId>
    <artifactId>h2sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Here is the application.properties file:这是 application.properties 文件:

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

Make the jar available in runtime.使 jar 在运行时可用。

Modify the scope for h2 dependency in the pom.xml :修改 pom.xml 中 h2 依赖的范围:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

You declare the h2 driver in the test scope which means it is only available during the test case execution.您在test范围内声明 h2 驱动程序,这意味着它仅在测试用例执行期间可用。

If you want the h2 driver is also available when executing the application normally , change its scope to the default one (ie compile ) should solve the problem :如果您希望 h2 驱动程序在正常执行应用程序时也可用,将其范围更改为默认范围(即compile )应该可以解决问题:

       <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
       </dependency>

In my case the problem was caused by some trailing blank characters after the driver classname in application.properties在我的情况下,问题是由 application.properties 中的驱动程序类名后的一些尾随空白字符引起的

This fails (dots in the code snippet below should be read as whitespace chars):这失败了(下面代码片段中的点应该读作空白字符):

spring.datasource.driverClassName=org.h2.Driver...

This succeeds:这成功了:

spring.datasource.driverClassName=org.h2.Driver

I have made all changes still not able to resolve, I tried restarting my project.我进行了所有更改仍然无法解决,我尝试重新启动我的项目。 All dependencies loaded and worked properly.所有依赖项都已加载并正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 gradle 时,在 Spring 启动测试期间无法加载驱动程序类:“org.h2.Driver” - Cannot load driver class: "org.h2.Driver" during spring boot tests when using gradle 无法加载驱动程序 class:org.h2.Driver - Cannot load driver class: org.h2.Driver LocalContainerEntityManagerFactoryBean无法加载JDBC驱动程序类&#39;org.h2.Driver&#39;无法构建Hibernate SessionFactory - LocalContainerEntityManagerFactoryBean Cannot load JDBC driver class 'org.h2.Driver' Unable to build Hibernate SessionFactory Spring Roo:&#39;org.h2.Driver&#39;不能使用JDBC驱动程序 - Spring Roo: JDBC driver not available for 'org.h2.Driver' Wildfly中的ClassNotFoundException org.h2.Driver - ClassNotFoundException org.h2.Driver in Wildfly org.h2.Driver 的 java ClassNotFoundException - java ClassNotFoundException for org.h2.Driver 是否可以在不设置Class.forName(“ org.h2.Driver”)的情况下连接到h2嵌入式数据库? - Can I connect to h2 embedded db without setting Class.forName(“org.h2.Driver”)? h2数据库class.forname(“ org.h2.Driver”)的ClassNotFound异常 - ClassNotFound exception for h2 database class.forname(“org.h2.Driver”) H2数据库org.h2.Driver ClassNotFoundException - H2 database org.h2.Driver ClassNotFoundException 无法加载类[org.postgresql.Driver] - Unable to load class [org.postgresql.Driver]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM