Spring Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:


1. Overview

Property files are a common method that we can use to store project-specific information. Ideally, we should keep it external to the packaging to be able to make changes to the configuration as and when required.

In this quick tutorial, we'll look into various ways to load the properties file from a location outside the jar in a Spring Boot application.

2. Using the Default Location

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml in 4 predetermined locations in the following order of precedence:

  • A /config subdirectory of the current directory
  • The current directory
  • A classpath /config package
  • The classpath root

Therefore, a property defined in application.properties and placed in the /config subdirectory of the current directory will be loaded. This will also override properties in other locations in case of a collision.

3. Using the Command Line

If the above convention doesn't work for us, we can also configure the location directly in the command line:

java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties

We can also pass a folder location where the application will search for the file:

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config

And, an alternative approach is running the Spring Boot application through the Maven plugin. There, we can use a -D parameter:

mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"

4. Using Environment Variables

Or, let's say that we can't alter the start-up command. What's great is Spring Boot will also read the environment variables SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION:

export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar

Note that the default file will still be loaded. But in case of a property collision, the environment-specific property file takes precedence.

5. Using Application Properties

As we can see, both spring.config.name and spring.config.location properties have to be defined before the application starts, hence using them in the application.properties file (or the YAML counterpart) will have no effect.

Spring Boot modified how properties are handled in version 2.4.0, and together with this change, the team introduced a new property that allows importing additional configuration files directly from the application properties:


6. Programmatically

Or, if we want programmatic access, we can register a PropertySourcesPlaceholderConfigurer bean:

public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer properties = 
      new PropertySourcesPlaceholderConfigurer();
    properties.setLocation(new FileSystemResource("/Users/home/conf.properties"));
    return properties;

Here, we've used PropertySourcesPlaceholderConfigurer to load the properties from a custom location.

7. Excluding a File from the Fat Jar

The Maven Boot plugin will automatically include all files in the src/main/resources directory to the jar package.

If we don't want a file to be part of the jar, then we can a simple configuration to exclude it:


In this example, we've filtered out the conf.properties file from being included in the resulting jar.

8. Conclusion

As we can see, the Spring Boot framework itself takes care of externalized configuration for us.

Often, we just have to place the property values in the correct files and locations, but we can also use Spring's Java API for more control.

As always, the full source code of the examples is available over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!