[spring-projects/spring-boot]@ConfigurationProperties 在 Spring Boot v3.0.x 之后无法正常工作(Kotlin v1.7.x +Spring Boot v3.0.4)

2024-05-08 619 views
5

大家好,我正在尝试使用 Spring Boot v3.0.4 连接我的 Web 应用程序中的多个数据源。但是,当我运行我的网络应用程序时,发生了以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: 
Failed to instantiate [org.flywaydb.core.Flyway]: 
Factory method 'flyway' threw exception with message: Error creating bean with name 'routingDataSource' defined in class path resource [CouponDbConfig.class]: 
Failed to instantiate [javax.sql.DataSource]: 
Factory method 'routingDataSource' threw exception with message: dataSource or dataSourceClassName or jdbcUrl is required.

这是我的源代码。

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.coupon.writer")
    fun couponDatasourceWriter(): DataSource = DataSourceBuilder.create().build()
   // ...

    @FlywayDataSource
    @Bean
    fun routingDataSource(): DataSource {
        val writerDataSource: DataSource = couponDatasourceWriter()
    //...
   }

这是我的 application.yml 文件。

spring:
  datasource:
    coupon:
      writer:
        driver-class-name: org.postgresql.Driver
        jdbc-url: jdbc:postgresql://localhost:xxx/test
        username: xxx 
        password: xxx

起初,我怀疑@ConfigurationProperties注释无法注入我的应用程序属性(myapplication.yml)。因此,我使用以下代码进行了测试,应用程序可以成功运行。

    @Bean
    fun couponDatasourceWriter(): DataSource = DataSourceBuilder.create()
        .driverClassName("org.postgresql.Driver")
        .url("jdbc:postgresql://localhost:xxx/test")
        .username("xxx")
        .password("xxx")
        .build()
   // ...

    @FlywayDataSource
    @Bean
    fun routingDataSource(): DataSource {
        val writerDataSource: DataSource = couponDatasourceWriter()
    //...
   }

问题是,在 Spring Boot v2.6.6 中,上述发生错误的代码已成功运行,没有出现任何问题。我认为 Spring Boot v3.0.x 版本本身可能存在问题。我发现有关@ConfigurationPropertiesSpring Boot v3.0.x 注释的一些问题不完全相同,但相似。大家可以检查一下这个问题吗?

回答

5

从您包含的代码片段来看,它似乎DataSourceBuilder.create().build()正在用于创建DataSource您希望应用配置属性的内容。我很惊讶它在 2.6 中有效。

我建议更改您的@Configuration以便注入DataSourceProperties并使用它们来创建构建器。您可以在https://docs.spring.io/spring-boot/docs/3.0.x/reference/htmlsingle/#howto.data-access.configure-two-datasources中找到示例。

如果这对您不起作用,您能否提供一个在 2.7 中工作但在 3.0 中失败的示例应用程序。

4

您好@philwebb,感谢您的评论。我使用 Spring Boot v2.7.0(+JVM 版本 11)和 Spring Boot v3.0.4(+JVM 版本 17)尝试了两种不同的方法。第一个是在Spring Boot v2.7.0中成功但在Spring Boot v3.0.4中失败的示例代码。

@Configuration(proxyBeanMethods = false)
class DbConfig {

    @Bean
    @Primary
    @FlywayDataSource
    @ConfigurationProperties(prefix = "spring.datasource.coupon.writer")
    fun couponDatasourceWriter(): DataSource = DataSourceBuilder.create().build()

}
spring.config.activate.on-profile: local
spring:

  datasource:
    coupon:
      writer:
        driver-class-name: org.postgresql.Driver
        jdbc-url: jdbc:postgresql://localhost:54322/couponWriter
        username: coupon
        password: coupon
      reader:
        driver-class-name: org.postgresql.Driver
        jdbc-url: jdbc:postgresql://localhost:54321/couponReader
        username: coupon
        password: coupon

第二个是 Spring Boot v2.7.0 和 v3.0.4 均失败的示例代码。

@Configuration(proxyBeanMethods = false)
class DbConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.coupon.writer")
    fun couponDatasourceProperty(): DataSourceProperties = DataSourceProperties()

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.coupon")
    fun firstDataSource(couponDatasourceProperty: DataSourceProperties): DataSource {
        return couponDatasourceProperty.initializeDataSourceBuilder().build()
    }
spring.config.activate.on-profile: local
spring:

  datasource:
    coupon:
      writer:
        driver-class-name: org.postgresql.Driver
        jdbc-url: jdbc:postgresql://localhost:54322/couponWriter
        username: coupon
        password: coupon
      reader:
        driver-class-name: org.postgresql.Driver
        jdbc-url: jdbc:postgresql://localhost:54321/couponReader
        username: coupon
        password: coupon

错误消息如下所示。

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine suitable jdbc url
Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (the profiles local are currently active).

确保没有其他设置,例如飞行路线。我刚刚测试了我的示例应用程序是否可以与我的本地 postgre 数据库连接。如果您需要有关此问题的更多信息,请告诉我。

我认为Spring Bean代理机制可能存在问题。

1

@AnneMayor 您能否将项目作为 zip 文件附加或共享 GitHub 存储库。

6

没问题,我已经为您附加了 zip 文件,@philwebb Spring Boot 2.7.x:demo.zip Spring Boot v3.0.x:demo-spring-boot-3.zip

谢谢 ?

4

感谢@AnneMayor 提供的示例,但它们似乎都以相同的方式失败:

这是基于 Boot 2.7 的应用程序:

$ ./gradlew bootRun         

> Task :bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

2023-03-28 16:55:38.017  INFO 90699 --- [           main] com.spring.demo.DemoApplicationKt        : Starting DemoApplicationKt using Java 17.0.6 on wilkinsonaGMD6R.vmware.com with PID 90699 (/Users/awilkinson/Downloads/demo/build/classes/kotlin/main started by awilkinson in /Users/awilkinson/Downloads/demo)
2023-03-28 16:55:38.019  INFO 90699 --- [           main] com.spring.demo.DemoApplicationKt        : No active profile set, falling back to 1 default profile: "default"
2023-03-28 16:55:38.439  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2023-03-28 16:55:38.440  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2023-03-28 16:55:38.445  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JDBC repository interfaces.
2023-03-28 16:55:38.451  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2023-03-28 16:55:38.451  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-03-28 16:55:38.455  INFO 90699 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 0 ms. Found 0 JPA repository interfaces.
2023-03-28 16:55:38.776  INFO 90699 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-03-28 16:55:38.783  INFO 90699 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-28 16:55:38.783  INFO 90699 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2023-03-28 16:55:38.863  INFO 90699 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-28 16:55:38.863  INFO 90699 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 815 ms
2023-03-28 16:55:38.898  WARN 90699 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'firstDataSource' defined in class path resource [com/spring/demo/DbConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'firstDataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2023-03-28 16:55:38.900  INFO 90699 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-03-28 16:55:38.911  INFO 90699 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-28 16:55:38.922 ERROR 90699 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

这是基于 3.0 的应用程序:

$ ./gradlew bootRun

> Task :bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.4)

2023-03-28T16:56:04.850+01:00  INFO 90770 --- [           main] c.d.d.DemoSpringBoot3ApplicationKt       : Starting DemoSpringBoot3ApplicationKt using Java 17.0.6 with PID 90770 (/Users/awilkinson/Downloads/demo-spring-boot-3/build/classes/kotlin/main started by awilkinson in /Users/awilkinson/Downloads/demo-spring-boot-3)
2023-03-28T16:56:04.852+01:00  INFO 90770 --- [           main] c.d.d.DemoSpringBoot3ApplicationKt       : No active profile set, falling back to 1 default profile: "default"
2023-03-28T16:56:05.216+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-03-28T16:56:05.216+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2023-03-28T16:56:05.225+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 JDBC repository interfaces.
2023-03-28T16:56:05.232+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-03-28T16:56:05.233+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-03-28T16:56:05.236+01:00  INFO 90770 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 JPA repository interfaces.
2023-03-28T16:56:05.532+01:00  INFO 90770 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-03-28T16:56:05.539+01:00  INFO 90770 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-28T16:56:05.539+01:00  INFO 90770 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-03-28T16:56:05.607+01:00  INFO 90770 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-28T16:56:05.608+01:00  INFO 90770 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 725 ms
2023-03-28T16:56:05.655+01:00  WARN 90770 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2023-03-28T16:56:05.656+01:00  INFO 90770 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-03-28T16:56:05.668+01:00  INFO 90770 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-03-28T16:56:05.678+01:00 ERROR 90770 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

您能否分享一些适用于 Spring Boot 2.7 但不适用于 Spring Boot 3.0 的内容?

2

如果您希望我们查看此问题,请提供所需的信息。如果在接下来的 7 天内未提供信息,此问题将被关闭。

0

哦,对不起。我不知道。我弄清楚后会告诉你的。谢谢。

4

如果您希望我们查看此问题,请提供所需的信息。如果在接下来的 7 天内未提供信息,此问题将被关闭。

8

回复晚了非常抱歉。您可以DbConfig.kt像下面这样重写代码并在 Spring boot v2.7 项目(演示)中重新运行它吗?它肯定会像我们预期的那样工作。

package com.spring.demo

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import javax.sql.DataSource

@Configuration(proxyBeanMethods = false)
class DbConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.coupon.writer")
    fun couponDatasourceWriter(): DataSource = DataSourceBuilder.create().build()
}
3

@philwebb 我今天发现了一个与此错误相关的问题。

我有一个默认设置为 true 的属性。我在文件中将此值覆盖为 false properties.yml

我挖了一下,发现binder.bind(bean)正在返回具有适当值的 bean,但这是错误的。但是,原始 bean 没有更新,注册的 bean 也没有更新。

我无法分享代码,但我添加了屏幕截图。可以看到,原始bean的实例和binder返回的bean并不相同。

截图 2023-04-13 15 37 04

我用两种不同的方法来初始化 ConfigurationProperties 进行了测试。正如您所看到的,用@Bean函数定义的函数不起作用。

在哪里application.yml

security.token.validation.application.required: true

TokenValidationConfig.kt


@ConfigurationProperties(prefix = "security.token.validation")
data class CustomEGTokenValidationSettings constructor(
    var application: JWKValidationConfig = JWKValidationConfig()
)

data class JWKValidationConfig(
    var required: Boolean = true
)

@Configuration
@EnableConfigurationProperties(CustomEGTokenValidationSettings::class)
class TokenValidationConfig {

    @Autowired
    private lateinit var customEGTokenValidationSettings: CustomEGTokenValidationSettings

    @Bean
    @ConfigurationProperties(prefix = "security.token.validation")
    fun issueCustomEGTokenValidationSettings() = CustomEGTokenValidationSettings()

    @Bean
    fun commandLineRunner(issueCustomEGTokenValidationSettings: CustomEGTokenValidationSettings) = CommandLineRunner {
        println(issueCustomEGTokenValidationSettings) // CustomEGTokenValidationSettings(application=JWKValidationConfig(required=true))
        println(customEGTokenValidationSettings) // CustomEGTokenValidationSettings(application=JWKValidationConfig(required=false))
    }
}
1

哇,太棒了@jmirc,我也会尽我所能仔细检查。太感谢了。

ps 如果我有机会贡献这个开源,我希望我能加入?

9

@AnneMayor 我再次尝试了你的 2.7.0 应用程序,并将DbConfig.它仍然失败:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

2023-05-09 15:29:56.015  INFO 6752 --- [           main] com.spring.demo.DemoApplicationKt        : Starting DemoApplicationKt using Java 17.0.6 on wilkinsonaGMD6R.vmware.com with PID 6752 (/Users/awilkinson/dev/temp/gh-34594/build/classes/kotlin/main started by awilkinson in /Users/awilkinson/dev/temp/gh-34594)
2023-05-09 15:29:56.017  INFO 6752 --- [           main] com.spring.demo.DemoApplicationKt        : No active profile set, falling back to 1 default profile: "default"
2023-05-09 15:29:56.494  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2023-05-09 15:29:56.496  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2023-05-09 15:29:56.504  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 JDBC repository interfaces.
2023-05-09 15:29:56.512  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2023-05-09 15:29:56.513  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-09 15:29:56.521  INFO 6752 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 JPA repository interfaces.
2023-05-09 15:29:56.935  INFO 6752 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-09 15:29:56.944  INFO 6752 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-09 15:29:56.944  INFO 6752 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2023-05-09 15:29:57.030  INFO 6752 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-09 15:29:57.030  INFO 6752 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 975 ms
2023-05-09 15:29:57.152 ERROR 6752 --- [           main] com.zaxxer.hikari.HikariConfig           : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.
2023-05-09 15:29:57.153  WARN 6752 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
2023-05-09 15:29:57.155  INFO 6752 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-05-09 15:29:57.168  INFO 6752 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-05-09 15:29:57.189 ERROR 6752 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.20.jar:5.3.20]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.20.jar:5.3.20]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.20.jar:5.3.20]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar:2.7.0]
        at com.spring.demo.DemoApplicationKt.main(DemoApplication.kt:13) ~[main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.20.jar:5.3.20]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.20.jar:5.3.20]
        ... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
        at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1029) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:109) ~[HikariCP-4.0.3.jar:na]
        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:159) ~[spring-jdbc-5.3.20.jar:5.3.20]
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:117) ~[spring-jdbc-5.3.20.jar:5.3.20]
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-5.3.20.jar:5.3.20]
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:330) ~[spring-jdbc-5.3.20.jar:5.3.20]
        at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:162) ~[spring-boot-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:130) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:143) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:103) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:95) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:132) ~[spring-boot-autoconfigure-2.7.0.jar:2.7.0]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.20.jar:5.3.20]
        ... 20 common frames omitted

您能否提供一个完整但最小的示例来重现您所描述的行为,而我们无需复制粘贴或修改任何内容?

9

@jmirc 我认为你的问题已经解决了。在 Spring Boot 3.0.4 中,我看到以下输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.4)

2023-05-09T15:43:45.841+01:00  INFO 11270 --- [           main] c.e.g.Gh34594JmircApplicationKt          : Starting Gh34594JmircApplicationKt using Java 17 with PID 11270 (/Users/awilkinson/dev/temp/gh-34594-jmirc/gh-34594-jmirc/build/classes/kotlin/main started by awilkinson in /Users/awilkinson/dev/temp/gh-34594-jmirc/gh-34594-jmirc)
2023-05-09T15:43:45.844+01:00  INFO 11270 --- [           main] c.e.g.Gh34594JmircApplicationKt          : No active profile set, falling back to 1 default profile: "default"
2023-05-09T15:43:46.630+01:00  INFO 11270 --- [           main] c.e.g.Gh34594JmircApplicationKt          : Started Gh34594JmircApplicationKt in 1.259 seconds (process running for 1.814)
CustomEGTokenValidationSettings(application=JWKValidationConfig(required=true))
CustomEGTokenValidationSettings(application=JWKValidationConfig(required=false))

对于 3.0.5(和 3.0.6),事情会按预期进行:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.5)

2023-05-09T15:44:55.933+01:00  INFO 11508 --- [           main] c.e.g.Gh34594JmircApplicationKt          : Starting Gh34594JmircApplicationKt using Java 17 with PID 11508 (/Users/awilkinson/dev/temp/gh-34594-jmirc/gh-34594-jmirc/build/classes/kotlin/main started by awilkinson in /Users/awilkinson/dev/temp/gh-34594-jmirc/gh-34594-jmirc)
2023-05-09T15:44:55.936+01:00  INFO 11508 --- [           main] c.e.g.Gh34594JmircApplicationKt          : No active profile set, falling back to 1 default profile: "default"
2023-05-09T15:44:56.435+01:00  INFO 11508 --- [           main] c.e.g.Gh34594JmircApplicationKt          : Started Gh34594JmircApplicationKt in 0.719 seconds (process running for 1.282)
CustomEGTokenValidationSettings(application=JWKValidationConfig(required=false))
CustomEGTokenValidationSettings(application=JWKValidationConfig(required=false))