SpringBoot 利用配置项完成热更操作

ConfigurationProperties 注解

①新增类

java 复制代码
@Data
@ConfigurationProperties(prefix = "xx.aaxx")
public class ContentProperties {

  /**
   * 内容
   */
  private String content;

}

②配置文件application.properties

properties 复制代码
xx.aaxx.content=热更内容

③使用

java 复制代码
  @Autowired
  private ContentProperties contentProperties;
  // 使用
  String content = contentProperties.getContent();

Value 注解

①配置文件application.properties

properties 复制代码
xx.aaxx.content=热更内容

②使用

java 复制代码
  @Value("${xx.aaxx.content:默认内容}")
  private String content;
  // 使用
  System.out.println(content)

就算配置文件application.properties 没有配置,也可以使用默认内容,这样启动就不会报错,如果没有默认值就必需写配置项

以上两种方式都可以实现:

无需更改代码去发布,只需要热更配置即可 像Nacos等就更加方便了,服务都不用重启

相关推荐
小溪学编程13 小时前
Java InputStream 详解:从基础到实战
java·python·php
血小板要健康13 小时前
队列 + 宽搜(BFS):二叉树层序遍历 算法总结
java·数据结构·笔记·算法·leetcode·宽度优先
A黄俊辉A13 小时前
【无标题】
java
BestHeaker13 小时前
跨企业接口对接:IQDS / CPK 数据解析与协作避坑指南(五)
java·服务器·前端
yume_sibai14 小时前
03-Rust 函数式编程特性(闭包 + Iterator + Option/Result + 链式调用)
开发语言·后端·rust
俊昭喜喜里14 小时前
C#中的func<>
java·前端·c#
highreport14 小时前
net报表工具对比:HighReport 与 FastReport
java·c#
吃饱了得干活14 小时前
Redis 不是死脑筋,它是一套“会进化”的存储系统
redis·后端
伩仁14 小时前
别再 HTTP 200 一把梭了:用 RFC 9457 Problem Details 给 FastAPI 错误响应"立规矩"
后端
MeetTanG14 小时前
Go 实战锦囊|errgroup:优雅地管理并发任务组
后端·go