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

相关推荐
h***59333 分钟前
SpringBoot中如何手动开启事务
java·spring boot·spring
q_19132846953 分钟前
基于SpringBoot2+Vue2的宠物健康医疗论坛系统
vue.js·spring boot·mysql·健康医疗·宠物·计算机毕业设计
倚肆10 分钟前
Java泛型详解:尖括号<>、通配符?与类型参数T
java
yuuki23323312 分钟前
【C语言&数据结构】二叉树的链式递归
c语言·数据结构·后端
韩风66614 分钟前
雪花id改多workerID依赖redis
java
BD_Marathon15 分钟前
Eclipse 代码自动补全设置
android·java·eclipse
L.EscaRC21 分钟前
深入解析SpringBoot中的循环依赖机制与解决方案
java·spring boot·spring·循环依赖
IT_陈寒29 分钟前
Spring Boot 3.2 性能翻倍秘诀:这5个配置优化让你的应用起飞🚀
前端·人工智能·后端
曾经的三心草42 分钟前
JavaEE初阶-网络原理1
java·网络·java-ee
q***48411 小时前
SpringBoot整合easy-es
spring boot·后端·elasticsearch