SpringBoot不重启,刷新配置文件信息 | 您所在的位置:网站首页 › springboot取配置文件值 › SpringBoot不重启,刷新配置文件信息 |
项目配置文件更改,不重启的情况下重载配置文件,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,会被告知仅能调起一次。 这里只写了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-actuatorSpringBoot 2.0.3.RELEASE以后的版本需要加配置 management.endpoints.web.exposure.include=refresh启动项目,查看配置,然后发送请求,然后再看配置是否成功刷新 # 这个是刷新接口 curl -X POST http://localhost:8080/actuator/refresh成功后会返回哪些属性的值有变化 借鉴博客:https://www.cnblogs.com/weihuang6620/p/13723219.html |
CopyRight 2018-2019 实验室设备网 版权所有 |