SpringBoot不重启,刷新配置文件信息 您所在的位置:网站首页 springboot取配置文件值 SpringBoot不重启,刷新配置文件信息

SpringBoot不重启,刷新配置文件信息

2024-02-02 17:54| 来源: 网络整理| 查看: 265

项目配置文件更改,不重启的情况下重载配置文件,emmmmm第一想法是刷新上下文可以实现,真香。

刷新配置信息 1. AbstractApplicationContext Refresh(不可用)2. RefreshScope 刷新配置

Spring-Boot版本: 2.1.17.RELEASE

1. AbstractApplicationContext Refresh(不可用)

        以下是refresh的部分源码,这里不分析了,可以看到做了很多准备,这个方法是不能实现上下文刷新的。         因为refresh只能被调起一次,启动类中多次调起会被catch住然后抛出异常。         那不在启动类中调起呢,会检测线程池,然后给你shut down掉         所以刷新上下文,一言难尽

this.prepareRefresh(); ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory(); this.prepareBeanFactory(beanFactory); try { this.postProcessBeanFactory(beanFactory); this.invokeBeanFactoryPostProcessors(beanFactory); this.registerBeanPostProcessors(beanFactory); this.initMessageSource(); this.initApplicationEventMulticaster(); this.onRefresh(); this.registerListeners(); this.finishBeanFactoryInitialization(beanFactory); this.finishRefresh(); } catch (BeansException var9) { if (this.logger.isWarnEnabled()) { this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9); } this.destroyBeans(); this.cancelRefresh(var9); throw var9; } finally { this.resetCommonCaches(); }

在启动时,调起refresh,会被告知仅能调起一次。 在这里插入图片描述 在请求方法中调起refresh,会检测线程池,然后shut down 在这里插入图片描述

2. RefreshScope 刷新配置

这里只写了SpringBoot 2.0.3.RELEASE 版本以后的demo 依赖:cloud依赖与SpringBoot版本不能差太大,不然启动报错

org.springframework.cloud spring-cloud-starter-config 2.1.0.RELEASE org.springframework.boot spring-boot-starter-actuator

SpringBoot 2.0.3.RELEASE以后的版本需要加配置

management.endpoints.web.exposure.include=refresh

启动项目,查看配置,然后发送请求,然后再看配置是否成功刷新

# 这个是刷新接口 curl -X POST http://localhost:8080/actuator/refresh

成功后会返回哪些属性的值有变化 在这里插入图片描述 注意,在Idea中,启动项目时,读取的配置文件不是工程的配置文件,是打包后的配置文件,别忽略这个了,不然影响结果。 在这里插入图片描述

借鉴博客:https://www.cnblogs.com/weihuang6620/p/13723219.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有