日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring cloud 自定义配置源及配置刷新

發布時間:2025/6/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring cloud 自定义配置源及配置刷新 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

通過自定義配置源可以接入自己的配置服務,配合ContextRefresher可以讓應用運行中自動更新配置。

實現PropertySourceLocator

/*** 自定義配置源*/ public class MyPropertySourceLocator implements PropertySourceLocator {@Overridepublic PropertySource<?> locate(Environment environment) {String msg = new SimpleDateFormat("HH:mm:ss").format(new Date());Map<String, Object> map = new HashMap<>();map.put("demo.diy.msg", msg);System.err.println("MyPropertySourceLocator, demo.diy.msg = " + msg);//spring自帶的一個簡單的map結構配置集合,也可以繼承PropertySource自定義MapPropertySource source = new MapPropertySource("my-source", map);return source;} }

配置類

@Configuration public class MyConfigBootstrapConfiguration {@Beanpublic MyPropertySourceLocator myPropertySourceLocator() {return new MyPropertySourceLocator();}}

用Java代碼聲明bean,還需要在resources/META-INF/spring.factories中聲明

org.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.netease.ag.demoweb.MyConfigBootstrapConfiguration

Spring中類似與Java SPI的加載機制。它在META-INF/spring.factories文件中配置接口的實現類名稱,然后在程序中讀取這些配置文件并實例化。這種自定義的SPI機制是Spring Boot Starter實現的基礎。

使用自定義配置

@RefreshScope //可更新 @Component @Data public class ValueConfig {@Value("${demo.copy.msg}")private String copyMsg;@Value("${demo.diy.msg}")private String diyMsg;public ValueConfig() {System.err.println("ValueConfig init");} }

application.properties中可以引用自定義配置

demo.copy.msg=${demo.diy.msg}

springboot應用啟動

@SpringBootApplication @RestController public class DemowebApplication {@Resourceprivate ValueConfig valueConfig;@Resourceprivate ContextRefresher contextRefresher;public DemowebApplication() {System.err.println("DemowebApplication init");}public static void main(String[] args) {SpringApplication.run(DemowebApplication.class, args);}@RequestMapping("/t")public String t() {return valueConfig.toString();}//更新bean屬性@RequestMapping("/r")public Object r() {return contextRefresher.refresh();}

啟動日志

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v1.5.2.RELEASE)MyPropertySourceLocator, demo.diy.msg = 17:18:22 ... DemowebApplication init ... ValueConfig init ...Tomcat started on port(s): 8080 (http)

查詢,多次請求返回一致

請求:http://localhost:8080/t 響應:ValueConfig(copyMsg=17:18:22, diyMsg=17:18:22)

更新

請求:http://localhost:8080/r 響應:["demo.diy.msg"]

日志輸出:

MyPropertySourceLocator, demo.diy.msg = 17:27:44

再次調用查詢接口,發現值改變,并且輸出日志

ValueConfig init

證明更新字段實際是重新生成了一個bean

轉載于:https://my.oschina.net/liujiest/blog/2254176

總結

以上是生活随笔為你收集整理的spring cloud 自定义配置源及配置刷新的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。