配置数据库出现循环依赖问题

问题环境:spring boot 2.0.7、spring cloud Finchley.SR2
当添加自动生成数据库表的配置(springboot2.0.x 以后就需要显式配置出来)

spring.datasource.schema=classpath:schema.sql
spring.datasource.initialization-mode=always

出现循环依赖问题

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

Description:

The dependencies of some of the beans in the application context form a cycle:

   servletEndpointRegistrar defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]
      ↓
   healthEndpoint defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]
      ↓
   org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration
      ↓
   dataSource
┌─────┐
|  scopedTarget.dataSource defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘

解决方法是当前应用添加配置

##======添加配置============================================
spring.cloud.refresh.refreshable=none

该方式 会导致 @Refresh不生效

根据循环依赖出现的提示,因该是spring boot2.0.x 版本Hikari默认连接池出现的,
所以手动配置数据库连接池durid
pom.xml

<!-- 添加Druid依赖 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

配置文件

##druid 连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT1FROMDUAL
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filters=stat,wall

备注

通过测试 2.1.x 以及2.2.x 没有该问题。

你可能感兴趣的:(spring,cloud,spring,boot,spring,boot,数据库)