Search code examples
spring-bootpropertiesargumentscommandline

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


I have a Maven/SpringBootApplication that takes its properties from a Spring config Server. I need to override the values of these properties using command line arguments. unfortunately, the properties keep the values provided by the config server and are not overridden by the command line arguments.

  • I have confirmed that the parameters are properly passed to the App as I can see being passed to SpringApplication.run.
  • I can see in the function ConfigurableApplicationContext of Spring Framework the environment carrying the arguments in environment.propertysources.propertySourceList.SimpleCommandLinePropertySource.source.optionArgs
  • If I try to set a Spring-defined value (e.g. --logging.level.org.springframework.web=TRACE) it works, meaning Spring logs traces

I read all possible threads on the subject but none seem to apply to my problem.

This is my Spring boot app (args are beeing passed to the SpringApplication)

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

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

Here is the component and the property

@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;
}

The parameter being passed is received by the app (extracted from debugger):

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

I expect the property converterUriOffice to have the value foo Instead it gets its value from the config server (http://localhost:3000/convert/office)


Solution

  • found following in the documentation 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.

    same problem here with the solution 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).