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

相关推荐
jiankeljx13 分钟前
Spring Boot实现多数据源连接和切换
spring boot·后端·oracle
Cache技术分享16 分钟前
354. Java IO API - 获取路径信息
前端·后端
二闹21 分钟前
别再死记硬背了!带你扒开*args和**kwargs的底裤
后端·python
printfall27 分钟前
src/cli/run-main.ts
后端
luanma15098032 分钟前
Spring 框架——@Retryable 注解与 @Recover 注解
java·前端·spring
阿Y加油吧33 分钟前
力扣打卡——day01
java·算法·leetcode
码路飞37 分钟前
Java 25 发了但更让我兴奋的是这个:Spring AI 让 Java 调大模型终于不用手写 HTTP 了
java·人工智能·spring
sinat_255487811 小时前
transient 修饰符·学习笔记
java·开发语言·spring
jwn9991 小时前
SQL Server2019下载及安装教程
java
虚拟世界AI1 小时前
Java服务器开发:零基础实战指南
java·servlet·tomcat