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

相关推荐
吴名氏.34 分钟前
Java转AI Agent教程(第十五周)政策时效:过期政策不该被召回,也不能被删掉
java·人工智能·python·ai·ai agent
一条Go39 分钟前
Kubernetes 多集群接入:kubeconfig 只进不出
后端·架构·github
文丑颜不良啊10 小时前
LeetCode2981~3000题目解析
java·leetcode
nice_lcj52011 小时前
【从零理解 Spring AI · 13】RAG 要解决什么:把“闭卷考试“变成“开卷“
java·spring·检索增强·rag·spring ai·ai应用开发
Csvn11 小时前
Nginx 静态站点与 location 匹配:优先级、alias、缓存头与 gzip
后端·nginx
小满zs12 小时前
Go语言第二十章(初探Gin)
后端·go
学长毕设12 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
小满zs12 小时前
Go语言第十九章(http标准库)
后端·go
这个昵称不容易13 小时前
SpringBoot 医疗病历交互系统的设计与实现:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设