6 Practical Ways to Load External Configurations in Spring Boot 3

This article demonstrates six different techniques for loading external configuration data into Spring Boot 3 applications, covering command‑line arguments, spring.config.import, @PropertySource, EnvironmentPostProcessor, @TestPropertySource, and a custom PropertySourceLocator with complete code examples and expected outputs.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
6 Practical Ways to Load External Configurations in Spring Boot 3

1. Introduction

In Spring Boot application development, as business complexity grows and multiple deployment environments are required, hard‑coded configuration becomes hard to maintain and risky to change. Spring Boot’s externalized configuration mechanism lets developers separate configuration from code, storing it in external files or systems for better decoupling.

This article introduces six ways to load custom external configuration data.

2. Practical Cases

2.1 Using Command Line

Command‑line arguments are the most direct method. No code changes are needed; values can be read with @Value or @ConfigurationProperties. Example startup command:

java -jar app.jar --pack.app.title=commandLineParam

Test component:

@Component
public class CommandLineArgumentRunner implements CommandLineRunner {
  @Value("${pack.app.title:}")
  private String title;
  @Override
  public void run(String... args) throws Exception {
    System.err.printf("Command Line Argument title: %s%n", title);
  }
}

Output:

Command Line Argument title: commandLineParam

2.2 Using spring.config.import

The spring.config.import property can import configuration files from the classpath or file system without further processing.

spring:
  config:
    import:
      - optional:file:/d:/pack/config.properties

Configuration file ( config.properties) is shown in the image below:

config.properties example
config.properties example

Test component:

@Component
public class ConfigImportRunner implements CommandLineRunner {
  @Value("${pack.app.title:}")
  private String title;
  @Override
  public void run(String... args) throws Exception {
    System.err.printf("spring.config.import title: %s%n", title);
  }
}

Output:

spring.config.import title: value loaded via spring.config.import

2.3 Using @PropertySource

@PropertySource

can load properties from the classpath or a file system path, but it cannot load YAML files without a custom PropertySourceFactory. Example configuration:

@Component
@PropertySource(value = {"classpath:pack.properties", "file:/d:/pack/other.properties"}, encoding = "UTF-8")
public class PackConfig {
}

Properties files:

#pack.properties
pack.app.version=1.0.0
#other.properties
pack.app.author=pack

Test component:

@Component
public class PropertySourceRunner implements CommandLineRunner {
  @Value("${pack.app.version}")
  private String version;
  @Value("${pack.app.author}")
  private String author;
  @Override
  public void run(String... args) throws Exception {
    System.err.printf("version: %s, author: %s%n", version, author);
  }
}

Output:

version: 1.0.0, author: pack

2.4 Custom EnvironmentPostProcessor

Implement EnvironmentPostProcessor to modify the Environment before the application context refreshes.

public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
  private final PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader();
  @Override
  public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    Resource resource = new FileSystemResource(new File("d:/pack/xg.properties"));
    try {
      environment.getPropertySources().addFirst(loader.load("xg", resource).getFirst());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Register in META-INF/spring.factories:

org.springframework.boot.env.EnvironmentPostProcessor=\
com.pack.way4.postprocessor.PackEnvironmentPostProcessor

Test component:

@Component
public class EnvPostProcessorRunner implements CommandLineRunner {
  @Value("${pack.app.env}")
  private String env;
  @Override
  public void run(String... args) throws Exception {
    System.err.printf("env: %s%n", env);
  }
}

Output:

env: prod

2.5 Using @TestPropertySource

This annotation is used in test environments to load external properties.

@SpringBootTest
@TestPropertySource("file:/d:/pack/test.properties")
public class AppTest {
  @Value("${pack.app.ocr}")
  private String ocr;
  @Test
  public void testLoadConfig() {
    System.err.println(ocr);
  }
}

Content of test.properties: pack.app.ocr=xxxooo Test output:

xxxooo

2.6 Custom PropertySourceLocator (Spring Cloud)

Introduce Spring Cloud dependency and implement PropertySourceLocator to provide custom properties.

@Configuration
public class PackPropertySourceLocator implements PropertySourceLocator {
  @Override
  public PropertySource<?> locate(Environment environment) {
    return new MapPropertySource("packxg", Map.of("pack.app.vendor", "xg_pack"));
  }
}

Register in META-INF/spring.factories:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.pack.way5.propertysourcelocator.PackPropertySourceLocator

Test component:

@Component
public class LocatorRunner implements CommandLineRunner {
  @Value("${pack.app.vendor}")
  private String vendor;
  @Override
  public void run(String... args) throws Exception {
    System.err.printf("pack.app.vendor: %s%n", vendor);
  }
}

Output:

pack.app.vendor: xg_pack
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentSpring BootConfigurationPropertiesEnvironmentPostProcessorExternal Configuration
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.