spring-boot - 从命令行参数覆盖 Spring 中的应用程序属性值
问题描述
我们知道我们可以通过 @Value 注释将配置外部化,就像在 Spring boot 项目中一样。
@Value("${max.routes}")
private int maxRoutes;
在这种情况下,我们在注释参数本身中给出默认值,以下方式,
@Value("${max.routes:10}")
private int maxRoutes;
我们可以在启动此应用程序时通过传递 VM 参数来覆盖该值吗?
例如,-Dmax.routes=20
。它会覆盖价值吗?
解决方案
是的,系统属性和命令行参数将覆盖这些属性值。
如果你像这样运行你的应用程序......
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
-Dmax.routes=20
...您不能使用甚至使用应用程序参数来覆盖您的属性--max.routes=20
。应用程序参数将具有最高优先级。
推荐阅读
- javascript - 在 Angular 中声明变量:不能绑定到“*ngIf”,因为它不是“div”的已知属性
- r - 比较几个表并创建一个新表,显示哪些变量使用 R 匹配
- mysql - 我正在寻找一种方法来消除列中的重复值,同时在 MySQL 中使用 row_number over 、 partition by
- log4j2 - 如何更改包含库中的日志记录级别?
- c++ - Visual Studio 2019:如果不先将其导入单独的模块 (ixx) 中,则无法在 cpp 文件中“导入”C++ 标准库声明
- github - 如何更改 GitHub 应用程序的 Heroku 堆栈?
- r - 如何创建函数以轻松多次操作数据?
- ios - Swift:添加子视图会阻止用户使用该应用程序。洛蒂动画
- flutter - 如何仅使用颤振制作自定义加载小部件
- node.js - VS Code 在断点处以只读方式打开文件