当前位置: 首页 > 知识库问答 >
问题:

Spring Boot升级后无法实例化自定义库的数据源

董谦
2023-03-14

我在具有自动配置的库中定义了自定义数据源:

@Configuration
@ConditionalOnProperty(
        name = ["spring.datasource.type"],
        havingValue = "org.example.datasource.connector.RoutingDataSource",
        matchIfMissing = true
)
@EnableConfigurationProperties(RoutingDataSourceConfig::class)
@Import(value = [
        RoutingDataSourceBeanPostProcessor::class,
        WriteRouteInterceptor::class
])
@EnableAspectJAutoProxy
class RoutingDataSourceAutoConfiguration{
        @Bean
        @ConditionalOnMissingBean(DataSource::class)
        fun dataSource(routingDataSourceConfig: RoutingDataSourceConfig): RoutingDataSource {
                return RoutingDataSource().also {
                        it.initDataSources(routingDataSourceConfig)
                }
        }

        @Bean
        @ConditionalOnBean(RoutingDataSource::class)
        fun writeDataSource(routingDataSource: RoutingDataSource) =
                routingDataSource.getWriteDataSource()

}

使用spring Factorys文件:

# Auto Configure
org.springframework.boot.autoconfigure.AutoConfigureBefore=\
  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.example.datasource.connector.RoutingDataSourceAutoConfiguration

数据源配置如下

spring:
    datasource:
        url: jdbc:postgresql://${POSTGRESQL_HOSTPORT}/chords?targetServerType=master&reWriteBatchedInserts=true
        routing:
            write:
                jdbcUrl: jdbc:postgresql://${POSTGRESQL_HOSTPORT}/chords?targetServerType=master&reWriteBatchedInserts=true
                username: ${POSTGRESQL_USERNAME}
                password: ${POSTGRESQL_PASSWORD}
                schema: chords
            read:
                jdbcUrl: jdbc:postgresql://${POSTGRESQL_HOSTPORT}/chords?loadBalanceHosts=true
                username: ${POSTGRESQL_USERNAME}
                password: ${POSTGRESQL_PASSWORD}
                schema: chords
                readOnly: true
        type: org.example.datasource.connector.RoutingDataSource

它在spring boot 2.4.2上运行良好。然而,当我将其升级到2.5.2时,我的应用程序没有启动,因为它无法创建数据源bean并引发异常

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' 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$Generic.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException: Unable to find suitable method for url
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    ... 21 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Generic.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException: Unable to find suitable method for url
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfUnique(DefaultListableBeanFactory.java:2063)
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.flyway(FlywayAutoConfiguration.java:117)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 22 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException: Unable to find suitable method for url
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    ... 40 common frames omitted
Caused by: org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException: Unable to find suitable method for url
    at org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException.throwIf(UnsupportedDataSourcePropertyException.java:36)
    at org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.getMethod(DataSourceBuilder.java:561)
    at org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.set(DataSourceBuilder.java:543)
    at org.springframework.boot.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:181)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Generic.dataSource(DataSourceConfiguration.java:150)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 41 common frames omitted

我该怎么弥补?

共有1个答案

柯苗宣
2023-03-14

在默认情况下,不会指定自动配置类的求值顺序,微小的更改(这种更改可能使哈希映射重新排序)可能会导致这种失败。您需要指示Boot按特定顺序评估类中的条件:

@自动配置之前(DataSourceAutoConfiguration::class)

(我认为这些是你的语义学意思,自己定义一个DataSourcebean,但你可能需要@AutoConfigrePost来代替。)

 类似资料:
  • 刚刚将我的MySQL RDS实例从自由层db.t2.micro升级为db.r3.large。相同的存储、相同的安全组、相同的用户组/凭据、相同的VPC、相同的endpoint名称、相同的一切。 在升级之前,我可以通过MySQL workbench远程访问同一个实例,根据AWS的说法,我的endpoint位置没有改变。 收到的错误消息: 无法连接到'blahblah.rds.amazonaws.co

  • AMH7.1官方升级备份软件更新后 已经无法备份数据库到阿里云 腾讯云 七牛云 更新了备份软件 就这样了!

  • 问题内容: 我已经在Google Store中有一个应用程序。我正在使用具有3个表的内置数据库,并在应用程序首次启动时将其复制。现在,我想升级到该应用程序并添加另一个表。下面是我的代码。 我想问几个问题。上面的代码未升级。 现在,如果我是该应用程序的新用户,是否需要编辑旧数据库并制作另一个CKRecording表,并用放置在资产中的当前数据库替换该数据库,或者上述代码也适用于新用户? 问题答案:

  • 我刚刚通过自制升级到Postgres10.3。我似乎无法通过“psql[数据库名称]”登录我的数据库。 我得到以下错误:

  • 我用这个命令升级了open ssh:sudo apt-get升级openssh-server 我重新启动了服务器,之后,我无法连接到任何数据库(Wordpress数据库,you'll数据库,phpmyadmin数据库) 对于WordPress我得到:错误建立数据库连接 对于yourls,我得到:数据库配置不正确,或无法连接到数据库 对于phpmyadmin我得到:mysqli_real_conne

  • 在2019年7月(版本2.20190722)中,添加了更编纂的字段API。 它旨在尽可能地向后兼容。 这意味着,如果您在2019年7月之前创建了自定义字段,则很有可能会继续工作。 在决定是否需要升级自定义字段之前,应通读“危险区域”部分,并对您的字段进行全面测试。 由于在2019年7月之前字段之间缺乏标准化,因此很难涵盖开发人员可能需要进行的所有更改。 本文档尝试涵盖所有可能的更改,但是如果本文档