Spring boot Oracle Gradle:无法加载驱动程序类:oracle.jdbc.OracleDriver

Posted

技术标签:

【中文标题】Spring boot Oracle Gradle:无法加载驱动程序类:oracle.jdbc.OracleDriver【英文标题】:Spring boot Oracle Gradle : Cannot load driver class: oracle.jdbc.OracleDriver 【发布时间】:2020-01-29 08:11:43 【问题描述】:

我正在创建 Spring Boot 应用程序并连接到 AWS 实例中的 Oracle DB。 我的配置是

spring.datasource.url= jdbc:oracle:thin:@xxxx.com:1521/xxxx
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.schema=xxxxxx
#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
server.port=8081

build.gradle 是

dependencies 
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
// https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc7
implementation 'com.oracle.jdbc:ojdbc7:12.1.0.2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

当我尝试运行该应用程序时,它出现以下错误。此错误显示未找到驱动程序类。我对错误的来源感到困惑。所以我在我的异常的堆栈跟踪下面发布。

    org.springframework.beans.factory.UnsatisfiedDependencyException: 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: oracle.jdbc.OracleDriver
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:392) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at com.saix.entitlement.EntitlementSaixApplication.main(EntitlementSaixApplication.java:10) [main/:na]
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: oracle.jdbc.OracleDriver

【问题讨论】:

【参考方案1】:

很难判断错误的确切来源。从错误信息来看,似乎是 Oracle 驱动程序没有正确解析。 Gradle 应该报告此类问题。

Oracle 最近刚刚开始将 JDBC 驱动程序发布到Maven Central。阅读 Oracle 开发人员博客上的官方公告:Oracle JDBC drivers on Maven Central。

长话短说,司机com.oracle.jdbc:ojdbc7:12.1.0.2不在其中。 MVN Repository 表示您定义的驱动程序已在 ICM(波兰大学)repository 上发布。只要您没有在构建文件中定义存储库,Gradle 就无法解析这个特定的驱动程序版本。

如果您可以使用最新的可用驱动程序,请尝试使用

implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'

如果您需要问题中提到的特定驱动版本,您可以尝试以下方法:

    在您的构建文件中定义 ICM repository 并让它从那里解析。 将驱动程序下载到本地 lib 目录或 Maven 存储库,然后让 Gradle 从那里获取驱动程序 (How to add local .jar file dependency to build.gradle file?)。 如果您有权访问某个驱动程序,请将相关驱动程序上传到私有 Maven 存储库实例(Nexus、Artifactory)。这是企业环境中的常见设置。

【讨论】:

【参考方案2】:

确保依赖项位于 maven central 中: http://repo1.maven.org/maven2/com/oracle/。 当我检查时我找不到它。 还要确保您使用的 mavenCentral() 或 mavenLocal() 具有您尝试下载的依赖项。验证构建后生成的 jar 是否包含 ojdbc7.jar 。如果它不存在,则 gradle 无法正确解析 jar,您需要检查配置。 还可以尝试在编译配置中使用驱动程序(以防你的 gradle 版本有问题):

compile ("com.oracle.jdbc:ojdbc7:12.1.0.2")

如果问题是因为 jar 不存在,请手动下载并将其添加到本地 maven 存储库,然后使用它进行构建以进行验证。

【讨论】:

以上是关于Spring boot Oracle Gradle:无法加载驱动程序类:oracle.jdbc.OracleDriver的主要内容,如果未能解决你的问题,请参考以下文章

spring boot gradle

QueryDSL、spring-boot 和 Gradle

spring boot + gradle[草稿]

飞舞的spring boot2(52)——Spring Boot Gradle插件

多模块 Spring Boot 项目中的 Gradle 依赖插件

使用Spring Boot Gradle插件的多模块项目