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

相关推荐
一 乐1 分钟前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
lhrimperial7 分钟前
企业智能知识库助手落地实践:从RAG到Multi-Agent
java·spring cloud·微服务·系统架构·知识图谱
3***688412 分钟前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
C***u17616 分钟前
Spring Boot问题总结
java·spring boot·后端
上进小菜猪16 分钟前
基于 YOLOv8 的人体与行人检测智能识别实战 [目标检测完整源码]
后端
Elieal30 分钟前
5 种方式快速创建 SpringBoot 项目
java·spring boot·后端
c***693032 分钟前
Spring Boot实时推送技术详解:三个经典案例
spring boot·后端·状态模式
better_liang38 分钟前
每日Java面试场景题知识点之-Java修饰符
java·访问控制·static·abstract·final·修饰符·企业级开发
rgeshfgreh1 小时前
Spring事务传播机制深度解析
java·前端·数据库
无名-CODING1 小时前
Java Spring 事务管理深度指南
java·数据库·spring