Springboot如何使用@RefreshScope注解实现配置文件的动态加载
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章主要为大家展示了"Springboot如何使用@RefreshScope注解实现配置文件的动态加载",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"S
千家信息网最后更新 2025年11月13日Springboot如何使用@RefreshScope注解实现配置文件的动态加载
这篇文章主要为大家展示了"Springboot如何使用@RefreshScope注解实现配置文件的动态加载",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Springboot如何使用@RefreshScope注解实现配置文件的动态加载"这篇文章吧。
spring-boot-starter-actuator提供服务健康检查和暴露内置的url接口。
spring-cloud-starter-config提供动态刷新的一些支持和注解。
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.6 com.xiaobu demo-for-mybatis-plus 0.0.1-SNAPSHOT demo-for-mybatis-plus demo-for-mybatis-plus 1.8 2020.0.3 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test asm org.ow2.asm com.baomidou mybatis-plus-boot-starter 3.4.2 org.projectlombok lombok 1.16.10 cn.hutool hutool-all 5.3.2 io.springfox springfox-swagger2 2.9.2 guava com.google.guava io.springfox springfox-swagger-ui 2.9.2 com.google.guava guava 29.0-jre com.alibaba easyexcel 2.0.2 junit junit com.xuxueli xxl-job-core 2.3.0 mysql mysql-connector-java org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-bootstrap org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import src/main/resources src/main/java **/*.xml true App org.springframework.boot spring-boot-maven-plugin 2.4.5
properties
########## Mybatis 自身配置 ##########logging.level.com.xiaobu=debugmybatis-plus.type-aliases-package=com.xiaobu.entitymybatis-plus.mapper-locations=classpath:com/xiaobu/mapper/xml/*.xml# 控制台打印sql 带参数 无法写入文件#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# 将sql 写入文件 带参数mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl#集成mysql数据库的配置spring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/master0?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghaispring.datasource.username=rootspring.datasource.password=root#测试动态刷新配置order.pay-timeout-seconds=9999order.create-frequency-seconds=600#暴露内置的刷新配置文件url,这个必须写,否则无法刷新配置文件management.endpoints.web.exposure.include=refresh#management.endpoints.web.exposure.include=env,refresh#management.endpoints.web.exposure.include=env,refresh
启动类
package com.xiaobu;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.ConfigurationPropertiesScan;/** * @author 小布 */@SpringBootApplication@ConfigurationPropertiesScanpublic 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@Datapublic class OrderProperties { /** * 订单支付超时时长,单位:秒。 */ private Integer payTimeoutSeconds; /** * 订单创建频率,单位:秒 */ 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")@RefreshScopepublic 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(); }}打包
执行
mvn clean package -Dmaven.test.skip=true
cmd启动jar 并指定外部配置文件
java -jar App.jar --spring.config.location=D:/application.properties
访问:http://localhost:8080/refresh/test
修改配置文件内容:

执行 POST http://localhost:8080/actuator/refresh
再次访问:http://localhost:8080/refresh/test
访问:http://localhost:8080/refresh/test01
springcloud对应的springboot版本

以上是"Springboot如何使用@RefreshScope注解实现配置文件的动态加载"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
配置
文件
动态
注解
内容
篇文章
小布
单位
参数
订单
学习
帮助
健康
再次
接口
控制台
数据
数据库
时长
易懂
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发及服务合同
dnf台服双数据库
服务器开机时间是什么意思
数据库流量怎么填
物联网网络技术特征
关于计算机软件开发论文
智能消防软件开发报价
在北京软件开发年薪四十多万
潍坊互联网养老软件开发
数据库查询日期中月份
lol服务器更新公告
数据库代金券合作伙伴
数据库自动备份可以删除吗
dcim服务器管理系统
网络安全宣传周论坛
ipcsun串口服务器
谷歌商店联系不到服务器
大数据和网络安全哪个方向
e4a连接sqlite数据库
曲阜师范大学软件开发研究生
经验反馈网络安全宣传周
蒲城市检察院检查网络安全
世界五大疾病数据库
武钢网络安全理念
中汽研场景数据库
幼儿园网络安全讲座简报
湖州物流软件开发设计
软件开发项目目的意义
为什么配置本地dns服务器
甘肃数据网络技术服务优势