简体   繁体   中英

Spring Boot - override property contained in a map (YAML)

I've recently been educated on how to properly override application properties (defined in application.yml) by using --<property>=<value> on the command line when launching the application. So far this has been working great.

Now I have configuration that is stored in a map structure. Internally (ie when read from application.yml) this works as expected, but my attempts to override on the command line have failed.

Here is the relevant configuration from application.yml:

services-config:
  common:
    connectionTimeout: 30000
    requestTimeout: 30000
    socketTimeout: 60000
    defaultKeepAlive: 20000
    defaultMaxConnPerRoute: 10
    maxTotalConnections: 200
  services:
    ? mission-planner
    : { id: mission-planner, name: Mission Planner, host: localhost, port: 8443 }

So, services is a map (or whatever YAML calls it), keyed by the value after the ? and the map entry value the value after the : .

In the code I can easily access the information, like the following:

ServiceConnectionProperties connProps = servicesConfig.getServices().get("mission-planner");
connProps.getHost();
connProps.getPort();

I had hoped that I would be able to override these properties using the following:

> java ... --services-config.services['mission-planner'].host=192.168.56.102 --services-config.services['mission-planner'].port=9400 ...

Unfortunately, this does not appear to work. The console log indicates that the host/port is being set to the values defined in application.yml (localhost:8443).

Am I using the wrong syntax to do the override? Is it just not possible to override items defined in a complex data structure? If anyone can help, it would be really appreciated.

Well, I found a way to make it work. I'm not 100% sure whether it's "correct" YAML or that there may be other, better ways to do this.

I changed the YAML as follows:

services-config:
  common:
    connectionTimeout: 30000
    requestTimeout: 30000
    socketTimeout: 60000
    defaultKeepAlive: 20000
    defaultMaxConnPerRoute: 10
    maxTotalConnections: 200
  services:
    mission-planner:
      id: mission-planner
      name: Mission Planner
      host: localhost
      port: 8443

I can override the "mission-planner" config as follows:

> java ... --services-config.services.mission-planner.host=192.168.56.102 --services-config.services.mission-planner.port=9400 ...

This works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM