Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載
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
修改配置文件內(nèi)容:
執(zhí)行 POST http://localhost:8080/actuator/refresh
再次訪問(wèn):http://localhost:8080/refresh/test
訪問(wèn):http://localhost:8080/refresh/test01
springcloud對(duì)應(yīng)的springboot版本
參考:
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)刷新配置
到此這篇關(guān)于Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載的文章就介紹到這了,更多相關(guān)Springboot @RefreshScope配置文件動(dòng)態(tài)加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SSM使用mybatis分頁(yè)插件pagehepler實(shí)現(xiàn)分頁(yè)示例
本篇文章主要介紹了SSM使用mybatis分頁(yè)插件pagehepler實(shí)現(xiàn)分頁(yè)示例,使用分頁(yè)插件的原因,簡(jiǎn)化了sql代碼的寫法,實(shí)現(xiàn)較好的物理分頁(yè),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-03-03Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解
這篇文章主要介紹了Spring框架IOC兩種創(chuàng)建工廠方法詳解,文中附含詳細(xì)的代碼示例分別對(duì)靜態(tài)方法和實(shí)例方法創(chuàng)建工廠作了簡(jiǎn)要的分析2021-09-09Java transient關(guān)鍵字與序列化操作實(shí)例詳解
這篇文章主要介紹了Java transient關(guān)鍵字與序列化操作,結(jié)合實(shí)例形式詳細(xì)分析了java序列化操作相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2019-09-09Spring中@EnableScheduling實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例
這篇文章主要介紹了Spring中@EnableScheduling實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例,@EnableScheduling 注解開啟定時(shí)任務(wù)功能,可以將多個(gè)方法寫在一個(gè)類,也可以分多個(gè)類寫,當(dāng)然也可以將方法直接寫在上面ScheddulConfig類中,需要的朋友可以參考下2024-01-01Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
比作MVC的話,控制器部分采用Servlet來(lái)實(shí)現(xiàn),模型部分采用JavaBean來(lái)實(shí)現(xiàn),而大部分的視圖采用Jsp頁(yè)面來(lái)實(shí)現(xiàn),接下來(lái)我們就來(lái)詳細(xì)看看如何用Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能2016-05-05詳細(xì)介紹Java阿里云的短信驗(yàn)證碼實(shí)現(xiàn)
這篇文章主要介紹了詳細(xì)介紹Java阿里云的短信驗(yàn)證碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05mybatisplus?isNotNull不生效問(wèn)題及解決
這篇文章主要介紹了mybatisplus?isNotNull不生效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06