springboot中怎么配置動(dòng)態(tài)刷新-創(chuàng)新互聯(lián)

springboot中怎么配置動(dòng)態(tài)刷新,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

為企業(yè)提供做網(wǎng)站、網(wǎng)站制作、網(wǎng)站優(yōu)化、成都全網(wǎng)營銷推廣、競價(jià)托管、品牌運(yùn)營等營銷獲客服務(wù)。成都創(chuàng)新互聯(lián)公司擁有網(wǎng)絡(luò)營銷運(yùn)營團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時(shí)降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!

具體做法如下:


1、pom:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.liuyx</groupId>  <artifactId>test-config-refresh</artifactId>  <version>1.0-SNAPSHOT</version>  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.4.RELEASE</version>    <relativePath/> <!-- lookup parent from repository -->  </parent>  <dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <!--監(jiān)控+refresh配置-->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>  </dependencies>  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Dalston.SR1</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement></project>

單獨(dú)引入 spring-boot-starter-actuator或者spring-cloud-starter-config(spring cloud config的客戶端) 是不會暴露/refresh端點(diǎn)的,兩者同時(shí)引入之后才能暴露/refresh端點(diǎn)。

2、一般使用spring-cloud-starter-config的文章都會讓你在bootstrap里加上配置中心服務(wù)端的地址,這里我們要脫離配置中心服務(wù)端使用,所以這些配置完全不需要。

3、對需要刷新的屬性使用@Value注解,同時(shí)將類使用@RefreshScope注解進(jìn)行標(biāo)記,示例如下:

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@RefreshScopepublic 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;  }}

這里我的變量是一個(gè)static變量,所以只能在非static的set方法上加@Value注解,而不是變量定義行的上方。如果不是靜態(tài)變量則可以直接寫作:

@Value("${server.port}")  private int port;

4、application.properties配置

server.port=80local.test=hello1management.security.enabled=false

5、測試

1、啟動(dòng)項(xiàng)目,訪問 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 測試成功

關(guān)于springboot中怎么配置動(dòng)態(tài)刷新問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站題目:springboot中怎么配置動(dòng)態(tài)刷新-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://bm7419.com/article10/iipgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、做網(wǎng)站品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)