Monday, November 5, 2018

Spring Boot override Bootstrap properties

1. Bootstrap properties file is inside the packaged jar


This is not a useful case to manage the bootstrap properties or application properties, the reason is you have to rebuild your application again if made any change to the properties

You should have properties file name following the format bootstrap-{profile}.yml ( or .properties variants)

# To to active profile you need to add the environment variable
mvn spring-boot:run -Dspring.profiles.active=production
# Or add a parameter to start application command
java -jar target/your_packaged.jar -Dspring.profiles.active=production

2. Bootstrap properties is outside of the packaged jar.


I prefer this case to manage properties, we don’t need to rebuild the application again if we made change to properties also we can manage our properties better with VCS like git.

Let assume that we have a configuration file outside of packaged jar.

In order to load the file, we need to change the bootstrap properties location by override the spring.cloud.bootstrap.location attribute.

@SpringBootApplication
@EnablePrometheusEndpoint
public class Application {
private static final String BOOTSTRAP_PATH =
"/data/project/project-code/config/bootstrap.yml";
public static void main(String[] args) {
loadBootstrapConfiguration();
SpringApplication.run(Application.class, args);
}
private static void loadBootstrapConfiguration() {
if (Files.exists(Paths.get(BOOTSTRAP_PATH))) {
System.setProperty("spring.cloud.bootstrap.location", BOOTSTRAP_PATH);
}
}
}


With this approach we can easily override the bootstrap properties.

No comments:

Post a Comment