Loading External Configuration Files with Spring Boot

Spring Boot provides a robust mechanism for loading external configuration files, allowing you to separate your application’s configuration from its codebase. In this tutorial, we will explore how to load multiple external configuration files using Spring Boot.

Introduction to External Configuration

External configuration is a crucial aspect of building scalable and maintainable applications. By separating your application’s configuration from its codebase, you can easily switch between different environments, such as development, testing, and production, without modifying the code.

Spring Boot supports various ways to load external configuration files, including:

  • Command line arguments
  • Java System properties (System.getProperties())
  • OS environment variables
  • JNDI attributes from java:comp/env
  • RandomValuePropertySource that only has properties in random.*
  • Application properties outside of your packaged jar (application.properties including YAML and profile variants)
  • Application properties packaged inside your jar (application.properties including YAML and profile variants)
  • @PropertySource annotations on your @Configuration classes
  • Default properties (specified using SpringApplication.setDefaultProperties)

Loading External Configuration Files

To load external configuration files, you can use the spring.config.location property. This property takes a comma-separated list of property files or file locations (directories).

For example, to add a directory that contains multiple configuration files, you can use the following command:

-Dspring.config.location=your/config/dir/

This will load all configuration files in the specified directory.

To load specific configuration files, you can specify their paths in the spring.config.location property. For example:

-Dspring.config.location=classpath:job1.properties,classpath:job2.properties

This will load the job1.properties and job2.properties files from the classpath.

Overriding Default Configuration

When loading external configuration files, Spring Boot allows you to override default configuration values. The default configuration files and locations are loaded before the additionally specified spring.config.location ones, meaning that the latter will always override properties set in the earlier ones.

To keep the default configuration values and add additional configuration files, you can use the spring.config.additional-location property. This property is available in Spring Boot 2.x and allows you to specify additional configuration locations without overriding the default ones.

Example Configuration

Let’s consider an example where we have a DBConfig class that loads database configuration from external properties files:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:jdbc-${spring.profiles.active}.properties")
public class DBConfig {
    @Value("${jdbc.host}")
    private String jdbcHostName;
}

In this example, we use the @PropertySource annotation to load the database configuration from a properties file named jdbc-${spring.profiles.active}.properties. The ${spring.profiles.active} placeholder is replaced with the active profile name.

To load external configuration files, you can run your application with the following command:

java -jar target/myapp.jar --spring.config.location=classpath:file:///C:/Apps/springtest/jdbc.properties,classpath:file:///C:/Apps/springtest/jdbc-dev.properties

This will load the jdbc.properties and jdbc-dev.properties files from the specified locations.

Conclusion

In this tutorial, we explored how to load multiple external configuration files using Spring Boot. We covered the different ways to load external configuration, including command line arguments, Java System properties, OS environment variables, and more. We also discussed how to override default configuration values and keep them when loading additional configuration files. By following these best practices, you can build scalable and maintainable applications with Spring Boot.

Best Practices

  • Use the spring.config.location property to load external configuration files.
  • Keep default configuration values in a separate file (e.g., application.properties) and override them with external configuration files as needed.
  • Use the spring.config.additional-location property to add additional configuration locations without overriding the default ones.
  • Use profile-specific properties files to manage different environments (e.g., development, testing, production).

Leave a Reply

Your email address will not be published. Required fields are marked *