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

相关推荐
SimonKing1 分钟前
局域网内跨平台传文件,没有比LocalSend更方便的了
java·后端·程序员
超级小龙虾2 分钟前
VS Code 本身没有内置像 IntelliJ IDEA 那样的完整书签(Bookmarks)
java
万物挽挽13 分钟前
单例模式的优点和缺点分别是什么?
java
Katherine_lin13 分钟前
基于Spring Boot的抽奖系统测试报告
java·spring boot·后端
C_lea14 分钟前
Java多线程学习(一)
java·学习
萧曵 丶16 分钟前
Java 线程池优化
java·开发语言
什么都不会的Tristan18 分钟前
SpringDataRedis
java·spring boot·spring
Seven9725 分钟前
剑指offer-58、对称二叉树
java
想唱rap29 分钟前
缓冲区的理解和实现
java·服务器·前端·c++·python
笃行客从不躺平30 分钟前
Spring Boot 的全局异常处理机制复习
java·spring boot·后端