首页 > 解决方案 > 从命令行参数覆盖 Spring 中的应用程序属性值

问题描述

我们知道我们可以通过 @Value 注释将配置外部化,就像在 Spring boot 项目中一样。

@Value("${max.routes}")
private int maxRoutes;

在这种情况下,我们在注释参数本身中给出默认值,以下方式,

@Value("${max.routes:10}")
private int maxRoutes;

我们可以在启动此应用程序时通过传递 VM 参数来覆盖该值吗?

例如,-Dmax.routes=20。它会覆盖价值吗?

标签: spring-boot

解决方案


是的,系统属性和命令行参数将覆盖这些属性值。

如果你像这样运行你的应用程序......

public static void main(String[] args) {
  SpringApplication.run(MyApplication.class, args);
}

-Dmax.routes=20...您不能使用甚至使用应用程序参数来覆盖您的属性--max.routes=20。应用程序参数将具有最高优先级。


推荐阅读