新聞中心
如何在spring boot中配置一個動態(tài)刷新功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務東方,十載網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108
1、pom:
4.0.0 com.liuyx test-config-refresh 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies Dalston.SR1 pom import
單獨引入 spring-boot-starter-actuator或者spring-cloud-starter-config(spring cloud config的客戶端) 是不會暴露/refresh端點的,兩者同時引入之后才能暴露/refresh端點。
2、一般使用spring-cloud-starter-config的文章都會讓你在bootstrap里加上配置中心服務端的地址,這里我們要脫離配置中心服務端使用,所以這些配置完全不需要。
3、對需要刷新的屬性使用@Value注解,同時將類使用@RefreshScope注解進行標記,示例如下:
package com.liuyx.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @RefreshScope public class Main { public static void main(String[] args) { SpringApplication.run(Main.class); } private static int port; @Value("${server.port}") public void setPort(int port){ this.port=port; } @RequestMapping("/port") public int port(){ return port; } }
這里我的變量是一個static變量,所以只能在非static的set方法上加@Value注解,而不是變量定義行的上方。如果不是靜態(tài)變量則可以直接寫作:
@Value("${server.port}") private int port;
4、application.properties配置
server.port=80 local.test=hello1 management.security.enabled=false
5、測試
1、啟動項目,訪問 http://localhost/port 顯示 80
2、修改classpath(注意是classpath,即你編譯后的class文件所處的目錄)下的application.properties將server.port改為801
3、發(fā)送空post(注意是post)請求到 http://localhost:80/refresh
4、再次訪問 http://localhost/port 顯示 801 測試成功
關于如何在spring boot中配置一個動態(tài)刷新功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
文章題目:如何在springboot中配置一個動態(tài)刷新功能
文章分享:http://fisionsoft.com.cn/article/iiopjj.html