一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

2021-12-24 12:49小布1994 Java教程

本文主要介紹了Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

spring-boot-starter-actuator提供服務(wù)健康檢查和暴露內(nèi)置的url接口。

spring-cloud-starter-config提供動(dòng)態(tài)刷新的一些支持和注解。

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.4.6</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.xiaobu</groupId>
  <artifactId>demo-for-mybatis-plus</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-for-mybatis-plus</name>
  <description>demo-for-mybatis-plus</description>
  <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>2020.0.3</spring-cloud.version>
  </properties>
  <dependencies>
      <!--spring boot-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
          <exclusions>
              <exclusion>
                  <artifactId>asm</artifactId>
                  <groupId>org.ow2.asm</groupId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.4.2</version>
      </dependency>
      <!-- lomback -->
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.16.10</version>
      </dependency>
      <dependency>
          <groupId>cn.hutool</groupId>
          <artifactId>hutool-all</artifactId>
          <version>5.3.2</version>
      </dependency>
      <!-- 引入Swagger2依賴 -->
      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.9.2</version>
          <exclusions>
              <exclusion>
                  <artifactId>guava</artifactId>
                  <groupId>com.google.guava</groupId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.9.2</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
      <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>29.0-jre</version>
      </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>easyexcel</artifactId>
          <version>2.0.2</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
      </dependency>

      <dependency>
          <groupId>com.xuxueli</groupId>
          <artifactId>xxl-job-core</artifactId>
          <version>2.3.0</version>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
      </dependency>
      <!--   spring-cloud config-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <!--        springcloud 高版本需要引入 spring-cloud-starter-bootstrap 否則刷新不起效-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-bootstrap</artifactId>
      </dependency>
  </dependencies>

  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>${spring-cloud.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>


  <build>
      <resources>
          <resource>
              <directory>src/main/resources</directory>
          </resource>
          <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.xml</include>
              </includes>
              <filtering>true</filtering>
          </resource>
      </resources>
      <finalName>App</finalName>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <version>2.4.5</version>
          </plugin>
      </plugins>
  </build>

</project>

 

properties

########## Mybatis 自身配置 ##########
logging.level.com.xiaobu=debug
mybatis-plus.type-aliases-package=com.xiaobu.entity
mybatis-plus.mapper-locations=classpath:com/xiaobu/mapper/xml/*.xml
# 控制臺(tái)打印sql 帶參數(shù) 無(wú)法寫入文件
#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 將sql 寫入文件 帶參數(shù)
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
#集成mysql數(shù)據(jù)庫(kù)的配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/master0?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#測(cè)試動(dòng)態(tài)刷新配置
order.pay-timeout-seconds=9999
order.create-frequency-seconds=600
#暴露內(nèi)置的刷新配置文件url,這個(gè)必須寫,否則無(wú)法刷新配置文件
management.endpoints.web.exposure.include=refresh
#management.endpoints.web.exposure.include=env,refresh#management.endpoints.web.exposure.include=env,refresh

 

啟動(dòng)類

package com.xiaobu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

/**
* @author 小布
*/
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoForMybatisPlusApplication {

  public static void main(String[] args) {
      SpringApplication.run(DemoForMybatisPlusApplication.class, args);
  }

}

 

配置類

package com.xiaobu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
* @author 小布
*/
@Component
@ConfigurationProperties(prefix = "order")
@RefreshScope
@Data
public class OrderProperties {
  /**
   * 訂單支付超時(shí)時(shí)長(zhǎng),單位:秒。
   */
  private Integer payTimeoutSeconds;

  /**
   * 訂單創(chuàng)建頻率,單位:秒
   */
  private Integer createFrequencySeconds;

}

 

controller

package com.xiaobu.controller;

import com.xiaobu.config.OrderProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* The type Refresh controller.
*
* @author 小布
* @version 1.0.0
* @className RefreshController.java
* @createTime 2021年09月06日 15:38:00
*/
@RestController
@RequestMapping("refresh")
@RefreshScope
public class RefreshController {

  @Autowired
  private OrderProperties orderProperties;

  @Value(value = "${order.pay-timeout-seconds}")
  private Integer payTimeoutSeconds;

  /**
   * Test string.
   *
   * @return the string
   */
  @GetMapping("test")
  public String test() {
      return "payTimeoutSeconds:" + payTimeoutSeconds;
  }

  @GetMapping("test01")
  public String test01() {
      return orderProperties.toString();
  }
}

 

打包

執(zhí)行

mvn clean package -Dmaven.test.skip=true

cmd啟動(dòng)jar 并指定外部配置文件

java -jar App.jar  --spring.config.location=D:/application.properties

訪問(wèn):http://localhost:8080/refresh/test

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

修改配置文件內(nèi)容:

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

執(zhí)行 POST http://localhost:8080/actuator/refresh

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

再次訪問(wèn):http://localhost:8080/refresh/test

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

訪問(wèn):http://localhost:8080/refresh/test01

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

 

springcloud對(duì)應(yīng)的springboot版本

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

 

參考:

springcloud對(duì)應(yīng)的springboot版本

Springboot 使用@RefreshScope 注解,實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

Spring boot 應(yīng)用實(shí)現(xiàn)動(dòng)態(tài)刷新配置

Spring Boot 指定外部啟動(dòng)配置文件

到此這篇關(guān)于Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載的文章就介紹到這了,更多相關(guān)Springboot @RefreshScope配置文件動(dòng)態(tài)加載內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/tanhongwei1994/article/details/120147010

延伸 · 閱讀

精彩推薦
  • Java教程Java8中Stream使用的一個(gè)注意事項(xiàng)

    Java8中Stream使用的一個(gè)注意事項(xiàng)

    最近在工作中發(fā)現(xiàn)了對(duì)于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7482021-02-04
  • Java教程xml與Java對(duì)象的轉(zhuǎn)換詳解

    xml與Java對(duì)象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對(duì)象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程升級(jí)IDEA后Lombok不能使用的解決方法

    升級(jí)IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級(jí),尋思已經(jīng)有好久沒(méi)有升過(guò)級(jí)了。升級(jí)完畢重啟之后,突然發(fā)現(xiàn)好多錯(cuò)誤,本文就來(lái)介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程20個(gè)非常實(shí)用的Java程序代碼片段

    20個(gè)非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個(gè)非常實(shí)用的Java程序片段,對(duì)java開發(fā)項(xiàng)目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程Java實(shí)現(xiàn)搶紅包功能

    Java實(shí)現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)搶紅包功能,采用多線程模擬多人同時(shí)搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙...

    littleschemer13532021-05-16
主站蜘蛛池模板: 亚洲成年男人的天堂网 | 91会员 | 成人永久免费福利视频网站 | 男人天堂网av | 日本xxx在线观看免费播放 | 男人猛激烈吃奶gif动态图 | 9966久久精品免费看国产 | 日韩免费在线视频观看 | 51国产午夜精品免费视频 | 男人晚上适合偷偷看的污污 | 精品久久久噜噜噜久久7 | caopren免费视频国产 | 黑人巨大vs北条麻妃在线 | 大乳女子一级毛片 | 免费观看在线aa | chinses台湾男同志hd | 叛佛 作者满栀小说免费阅读 | 国产精品久久久久久久久久久久久久 | 欧美高清在线精品一区 | 热久久最新网址 | t66y地址一地址二地址三 | 青涩体验在线观看未删减 | 久久机热免费视频 | 菠萝视频5正版在线观看 | 天天有好逼| 色鬼网 | 99re5在线精品视频热线 | 国产欧美一区二区精品久久久 | 亚洲福利精品电影在线观看 | 久草在线精彩免费视频 | 99超级碰碰成人香蕉网 | chinese东北痞子gay | 欧美又大又粗又长又硬 | 精品亚洲午夜久久久久 | 欧美日韩亚毛片免费观看 | 极品 女神校花 露脸91 | 四虎永久视频 | 国产高清在线不卡 | 精品久久久久久久久免费影院 | 美女被到爽流动漫 | 青青青手机在线视频 |