Spring Boot and Multiple External Configuration Files

Multiple external property file in spring boot from command line

This is how u can load properties from any location using environment variable

-Dspring.config.location="C:\Project\properties\", -Dsecure.properties.location="C:\Project\properties\security\dev\"

@PropertySources({
@PropertySource("file:${spring.config.location}/configuration.properties"),
@PropertySource("file:${secure.properties.location}/secure.properties")})

How does spring handle multiple property-files?

I may be late here, but ran into similar issue. The interesting point is springboot doesn't handle the yml file same way as it does .properties. For @PropertySource to load the yml correctly you need to provide the factory class.

Also, you are right about not using the application.yml in the library project as it will conflict with the later application referencing this library project.

Few good references,

Spring reference - https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-exposing-yaml-to-spring

For implementation reference - https://www.baeldung.com/spring-yaml-propertysource

What is the point of creating external configuration file in spring boot?

The point of application.properties is to be able to change some properties without re-compiling your code. So in big companies no QA phase, delivery needed

You just change some properties, restart the application and ready to go.

Externalizing configuration in Spring Boot with multiple applications running in the same container

The suggestions of @Iulian Rosca to use a pattern like ${properties_home}/${application_id}/application.properties brought me to the idea of defining a custom JVM property like app.config.root and using this property to override spring.config.location very early in the application lifecycle.

My application class looks now like this and works for embedded and container deployments:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApplication(builder);
}

public static void main(String[] args) {
configureApplication(new SpringApplicationBuilder()).run(args);
}

private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
return builder
.sources(Application.class)
.properties("spring.config.location:${${app.config.root}/myapp1/:#{null}}");
}

}

Important notes to this solution:

  • app.config.root has to be set externally by JVM or JNDI property
  • app.config.root can only contain a single external configuration path (for my requirements this was sufficient) in contrast to spring.config.location where multiple comma-separated paths can be specified
  • SpringApplicationBuilder.properties(...) sets the application's default properties. Because of this, spring.config.location cannot be specified externally anymore, as JVM or JNDI Properties have priority over default properties and would therefore override spring.config.location again.


Related Topics



Leave a reply



Submit