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.
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=commandLineParamTest 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: commandLineParam2.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.propertiesConfiguration file ( config.properties) is shown in the image below:
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.import2.3 Using @PropertySource
@PropertySourcecan 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=packTest 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: pack2.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.PackEnvironmentPostProcessorTest 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: prod2.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:
xxxooo2.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.PackPropertySourceLocatorTest 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_packSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
