【问题标题】:Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName引起:java.lang.IllegalArgumentException:驱动程序类名需要jdbcUrl
【发布时间】:2021-09-20 09:20:17
【问题描述】:

我的 spring boot 测试在 maven 构建期间抛出以下错误。

"level":"ERROR","logger":"com.zaxxer.hikari.HikariConfig","msg":"HikariPool-1 - jdbcUrl is required with driverClassName."

堆栈跟踪:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerFactoryBean' defined in class path resource
[com/pit/SchedulerConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: jdbcUrl is requir
ed with driverClassName.
Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.

在 MyApplicationTest.java 中引发错误的方法

@SpringBootTest
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
@EnableConfigurationProperties(value = MyConfig.class)
class MyApplicationTest {

   @Test
   void contextLoads() {
   assertNotNull(myController);

   }
}

我的 pom 在类路径中有这些依赖项。

  • spring-boot-starter-web
  • spring-boot-starter-test
  • spring-integration-sftp
  • postgresql
  • junit
  • 石英调度器

我的数据源 bean 配置如下

@Configuration
public class MyDataSourceCfg {

  private DataSource appDataSource() {
    return DataSourceBuilder.create()
          .driverClassName("org.postgresql.Driver")
          .url(env.getProperty("url") // values are set from Environment
          .username(env.getProperty("user"))
          .password(env.getProperty("password")
          .build();
  }
}

请告诉我发生了什么以及如何避免此错误。如果您有多个数据库但我只有一个 postgres 数据库,我阅读了有关以不同方式配置数据源的信息。请分享您的想法。

【问题讨论】:

  • .url(env.getProperty("url") 似乎没有 JDBC URL “jdcb: ...”。错误通常位于带有注释的行。
  • @JoopEggen 属性从保险库中提取并在启动期间放入环境中。 “jdbc:postgresql://...” 是我配置它的方式。如果我在构建期间跳过测试,我可以启动应用程序并运行。
  • 也许单元测试有不同的配置? Spring 通常有一个应用程序上下文和一个应用程序上下文测试。并且测试通常针对不同的数据库。
  • [ERROR] contextLoads Time elapsed: 0 s <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext 我在堆栈错误上方看到的内容。已将测试类放在问题中。除了一个其他自动装配的 bean 之外,该类中没有其他内容。
  • 抱歉,我帮不上忙。您可以从硬编码字符串开始:.url("jdbc: ..."),然后进行日志记录。

标签: java spring spring-boot


【解决方案1】:

考虑到您拥有所有相关的依赖项。 使用 Spring Data JPA 连接到 PostgreSQL 数据库,例如:-

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

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

application.properties 文件中:

spring.datasource.url=jdbc:postgresql://localhost:5432/dev
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect

借助 SpringBoot 强大的自动配置,您无需显式创建 DataSource bean,如果需要,可以使用这些属性值。

【讨论】:

  • 如果我知道如何从应用程序 yaml 文件中的 Environment 类访问属性,我可以使用它,因为在从不同来源启动期间所有值都被放入 Enrvironment 映射。另请注意,当我对详细信息进行硬编码时,问题已解决。
猜你喜欢
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2018-09-12
  • 2021-05-17
相关资源
最近更新 更多