Spring-boot @Value 属性未被命令行参数覆盖

Spring-boot @Value properties not overridden by command line arguments

我有一个 Maven/SpringBootApplication 从 Spring 配置服务器获取其属性。我需要使用命令行参数覆盖这些属性的值。不幸的是,这些属性保留了配置服务器提供的值,并且不会被命令行参数覆盖。

我阅读了关于该主题的所有可能线索,但 none 似乎适用于我的问题。

这是我的 Spring 启动应用程序(正在将参数传递给 Spring 应用程序)

@SpringBootApplication
@ComponentScan("com.mycompany")
public class App {

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

这是组件和 属性

@Component
public class TaskProcessor implements com.mycompnay.fwk.task.engine.TaskProcessor {

  private RestTemplate restTemplate = new RestTemplate();

  @Value("${mycompany.converter.converter-uri.office}")
  private String converterUriOffice;
}

传递的参数被应用程序接收(从调试器中提取):

0:"--debug=true"
1:"--logging.level.org.springframework.web=TRACE"
2:"--mycompany.converter.converter-uri.office=foo"
hash:0
value:char[44]@25

我希望 属性 converterUriOffice 的值为 foo 相反,它从配置服务器 (http://localhost:3000/convert/office)

获取其值

在文档中找到以下内容 https://cloud.spring.io/spring-cloud-static/Edgware.SR2/single/spring-cloud.html#overriding-bootstrap-properties

Overriding the Values of Remote Properties The property sources that are added to you application by the bootstrap context are often "remote" (e.g. from a Config Server), and by default they cannot be overridden locally, except on the command line. If you want to allow your applications to override the remote properties with their own System properties or config files, the remote property source has to grant it permission by setting spring.cloud.config.allowOverride=true (it doesn’t work to set this locally). Once that flag is set there are some finer grained settings to control the location of the remote properties in relation to System properties and the application’s local configuration: spring.cloud.config.overrideNone=true to override with any local property source, and spring.cloud.config.overrideSystemProperties=false if only System properties and env vars should override the remote settings, but not the local config files.

同样的问题与解决方案 https://github.com/spring-cloud/spring-cloud-config/issues/907

The documentation is not very clear cause it says that command line arguments always take precedence over remote configuration.

The property sources that are added to you application by the bootstrap context are often "remote" (e.g. from a Config Server), and by default they cannot be overridden locally, except on the command line.

To achieve that, you have to activate the spring.cloud.config.override-system-properties=false configuration (the documentation only talk about System properties but it seems to be applicable to command line arguments too).

来自 Devilluminati 的链接完成了这项工作。非常感谢!为了尽可能清楚,这是我必须做的。

1- 我的应用程序有一个匹配的 YML 文件,由名为 application.yml

的配置服务器提供

2- 在 application.yml 中,我有两个配置文件,我只希望能够在使用本地配置文件时覆盖参数。 所以这是我必须添加到 application.yml:

spring:
  profiles: local
  cloud:
    config:
      override-system-properties: false 

完成后(并重新启动配置服务器以确保它提取最新的 YML),我可以通过将以下内容传递到命令行来覆盖上面的值:

--mycompany.converter.converter-uri.office=foo