First, in order to use the configuration from an application.yml oder application.properties, its handy to abstract that with a Spring Component class.
@Component
@ConfigurationProperties("address")
public class AddressConfiguration {
private String city;
private String street;
public AddressConfiguration(String city, String street) {
this.city = city;
this.street = street;
}
public String getCity() {
return city;
}
public String getStreet() {
return street;
}
public void setCity(String city) {
this.city = city;
}
public void setStreet(String street) {
this.street = street;
}
}
The above class maps to the following application.yml:
address:
city: "New York"
The name that is specified for the ConfigurationProperties annotation must match with the root element name of your configuration, that you want to load. As you can see, the application.yml doesn't contain an entry for street. You can set additional entries (or overwrite existing ones) by supplying a SPRING_APPLICATION_JSON environment variable:
export SPRING_APPLICATION_JSON='{"address":{"street":"Broadway"}}'
If you are running Spring Boot inside a Docker container, you can pass the exported SPRING_APPLICATION_JSON environment variable to the container by providing the -e flag:
docker run -e SPRING_APPLICATION_JSON="$SPRING_APPLICATION_JSON"
No comments:
Post a Comment