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等就更加方便了,服务都不用重启

相关推荐
lUie INGA5 小时前
在2023idea中如何创建SpringBoot
java·spring boot·后端
geBR OTTE6 小时前
SpringBoot中整合ONLYOFFICE在线编辑
java·spring boot·后端
Porunarufu6 小时前
博客系统UI自动化测试报告
java
NineData6 小时前
NineData 新增支持 GaussDB 到 StarRocks 实时数据复制能力
后端
sghuter6 小时前
数字资源分发架构解密
后端·架构·dubbo
小码哥_常7 小时前
Spring Boot启动慢?这5个优化点带你起飞
后端
NineData7 小时前
NineData将亮相DACon 2026上海站!解锁AGI时代数据“智理”新范式
数据库·后端·架构
of Watermelon League7 小时前
SpringBoot集成Flink-CDC,实现对数据库数据的监听
数据库·spring boot·flink
Aurorar0rua7 小时前
CS50 x 2024 Notes C - 05
java·c语言·数据结构