SpringBoot兩種方式刷新配置信息
一、第一種方式
@?ConfigurationProperties?不能自動(dòng)刷新,需要手動(dòng)調(diào)用contextRefresher.refresh()方法來(lái)刷新配置。
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "biz")
public class BizConfig {
private String key;
private Long refresh;
//省略 gettersetter...
}二、第二種方式
@RefreshScope 注解可以使得這個(gè)類(lèi)具備刷新功能,可以在配置文件內(nèi)容變更后,刷新并更新這個(gè)配置類(lèi)的屬性值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/**
*
* @RefreshScope 注解可以使得這個(gè)類(lèi)具備刷新功能,可以在配置文件內(nèi)容變更后,刷新并更新這個(gè)配置類(lèi)的屬性值。使用@RefreshScope注解,僅僅只能刷新@Value的配置屬性。
*
* 在這個(gè)配置類(lèi)中,使用了 @Value 注解注入了一個(gè)屬性 uuid,該屬性的值來(lái)源于配置文件中 rest.uuid 屬性的值。
* 由于該配置類(lèi)使用了 @RefreshScope 注解,所以如果配置文件中 rest.uuid 的值發(fā)生變化,
* Spring Cloud Config Server 就會(huì)通知該配置類(lèi),然后調(diào)用 setUuid() 方法刷新該屬性的值,從而實(shí)現(xiàn)了動(dòng)態(tài)刷新配置的效果。
*
*
* 如果使用@RefreshScope注解,僅僅只能刷新@Value的配置屬性,
* 而對(duì)于@ConfigurationProperties則不能自動(dòng)刷新,需要手動(dòng)調(diào)用contextRefresher.refresh()方法來(lái)刷新配置。
*
* 因此,在DemoController中的refresh()方法,通過(guò)開(kāi)啟一個(gè)新的線程來(lái)異步調(diào)用contextRefresher.refresh()方法,
* 實(shí)現(xiàn)對(duì)@ConfigurationProperties的刷新。這樣就可以保證對(duì)于@Value和@ConfigurationProperties兩種配置屬性都可以進(jìn)行刷新。
*/
@RefreshScope
@Component
public class ValueConfig {
@Value("${rest.uuid}")
private String uuid;
// 省略 gettersetter
}使用 contextRefresher.refresh() 刷新
import com.alibaba.fastjson.JSONObject;
import com.lfsun.bootdynamicrefresh.config.BizConfig;
import com.lfsun.bootdynamicrefresh.config.ValueConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private ContextRefresher contextRefresher;
@Autowired
private BizConfig bizConfig;
@Autowired
private ValueConfig valueConfig;
/**
* 查詢配置信息
*/
@GetMapping(path = "/show")
public String show() {
JSONObject res = new JSONObject();
res.put("biz", JSONObject.toJSONString(bizConfig));
res.put("uuid", valueConfig.getUuid());
return res.toJSONString();
}
/**
* 刷新配置信息
*/
@GetMapping(path = "/refresh")
public String refresh() {
// 新開(kāi)一個(gè)線程進(jìn)行配置信息的刷新,避免阻塞其他請(qǐng)求的處理
new Thread(() -> contextRefresher.refresh()).start();
// 返回刷新后的配置信息
return show();
}
}配置文件 application.yml
biz:
refresh: ${random.long}
key: refresh-test
rest:
uuid: ${random.uuid}
server:
port: 8081pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>實(shí)現(xiàn)效果

http://localhost:8081/refresh/

到此這篇關(guān)于SpringBoot兩種方式刷新配置信息的文章就介紹到這了,更多相關(guān)SpringBoot刷新配置信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Intellij?IDEA?中調(diào)試?maven?插件的步驟
這篇文章主要介紹了Intellij?IDEA?中調(diào)試?maven?插件,本文分步驟給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
詳解使用Maven開(kāi)發(fā)Web應(yīng)用詳細(xì)步驟
這篇文章主要介紹了詳解使用Maven開(kāi)發(fā)Web應(yīng)用詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java泛型類(lèi)、重構(gòu)的使用及說(shuō)明
文章介紹了泛型類(lèi)和重構(gòu)的概念、原理和應(yīng)用,泛型類(lèi)是一種參數(shù)化類(lèi)型,可以操作不同數(shù)據(jù)類(lèi)型,而重構(gòu)是通過(guò)對(duì)已有代碼進(jìn)行修改,改善其設(shè)計(jì)、結(jié)構(gòu)和可讀性的過(guò)程,重構(gòu)可以幫助提高代碼質(zhì)量、簡(jiǎn)化理解與調(diào)試、優(yōu)化性能、改進(jìn)設(shè)計(jì)、提高團(tuán)隊(duì)協(xié)作并擴(kuò)展功能2025-11-11
為什么禁止在SpringBoot項(xiàng)目中使用@Autowired注解
寫(xiě)代碼的時(shí)候突然發(fā)現(xiàn)?idea?在屬性注入的?@Autowired?注解上給出警告提示,spring?framework?4.0?以后開(kāi)始出現(xiàn)的,spring?4.0?開(kāi)始就不推薦使用屬性注入,改為推薦構(gòu)造器注入和?setter?注入,推薦的方法是使用基于構(gòu)造函數(shù)和基于setter的依賴注入2024-08-08
Java中使用回調(diào)函數(shù)的方法實(shí)例
本文主要介紹了Java中使用回調(diào)函數(shù)的方法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
java實(shí)現(xiàn)讀取jar包中配置文件的幾種方式
本文主要介紹了java實(shí)現(xiàn)讀取jar包中配置文件的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

