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

相关推荐
小北方城市网3 分钟前
SpringBoot 集成 Elasticsearch 实战(全文检索与聚合分析):打造高效海量数据检索系统
java·redis·分布式·python·缓存
IMPYLH5 分钟前
Lua 的 Table 模块
开发语言·笔记·后端·junit·游戏引擎·lua
csdn_aspnet6 分钟前
.NET 10 中的 ASP.NET Core:Blazor、API 和 OpenAPI 的重大更新
后端·asp.net·.net·.net10
一个处女座的程序猿O(∩_∩)O6 分钟前
深入剖析Java线程生命周期:从创建到销毁的全流程详解
java·开发语言
一嘴一个橘子11 分钟前
mybatis - 多表映射(对一映射、对多映射)
java·mybatis
Albert Edison15 分钟前
【ProtoBuf】初识 protobuf
java·开发语言·protobuf
码出财富8 小时前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
我是小疯子669 小时前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
森叶9 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
二哈喇子!9 小时前
Eclipse中导入外部jar包
java·eclipse·jar