3

How do I change the values of application.yaml at runtime e.g I have a server address property that I would like to change at runtime

server: address: 192.168.1.100

Daniel Naves
  • 126
  • 1
  • 7
  • Ideally you might have a config server who provides all configs, you can take a look https://www.baeldung.com/spring-cloud-configuration – Jonathan JOhx Aug 02 '19 at 15:33
  • 2
    Possible duplicate of [Set/override Spring / Spring Boot properties at runtime](https://stackoverflow.com/questions/27919270/set-override-spring-spring-boot-properties-at-runtime) – Arthur Attout Aug 02 '19 at 15:36

2 Answers2

3

I assume this is a spring application, if so you can use the jvm arguments (-D) to override the values from application.yaml file. e.g application.yaml

server:
  address: 192.168.0.1

cmd line

java -jar -Dserver.address=10.10.0.1
0

If you need to override many properties you can also use a separate file

@SpringBootApplication
@PropertySources({
        @PropertySource(name = "default", value = "classpath:default.yaml"),
        @PropertySource(name = "external", value = "file:${custom.properties:}", ignoreResourceNotFound = true)
})
public class BootApplication { ... }

And launch like

java -jar -Dcustom.properties=/path/to/custom.yaml

Where custom.yaml contains

server: 
   address: 10.10.10.100
   port: 8888
   etc: blabla
   ...
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42