5 Essential Spring Boot 3 Techniques for Validating and Customizing Configuration Properties
This guide demonstrates five practical Spring Boot 3 techniques—including using @ConfigurationProperties with validation, handling missing @Value entries, defining custom property sources, enforcing required properties, and parsing YML files—to ensure robust configuration management and prevent startup failures.
Environment: Spring Boot 3.0.9
1. Validate Property Configuration
The @ConfigurationProperties annotation binds configuration properties to a Java object, and combined with @Validated it enables safe validation of those properties.
<code>@ConfigurationProperties(prefix = "pack.bank")
@Validated
public class ArmyProperties {
private String publicKey;
@NotEmpty(message = "主机不能为空")
private String host;
private Integer port;
// @ByteLength is a custom annotation
@ByteLength(leng = 6, message = "发送方系统节点号只能为6个字节")
private String sendNo;
@ByteLength(leng = 6, message = "接收方系统节点号只能为6个字节")
private String receiveNo;
/**是否加密*/
@EnumValue(intValues = {0, 1}, message = "是否加密只能是0或1")
private Integer cipher;
}</code>This approach prevents incorrect configurations; the application will fail to start if validation errors occur.
2. Validate Missing Properties
The @Value annotation injects configuration values into beans. If the property is absent, startup fails.
<code>@Value("${pack.name}")
private String name;</code>Provide a default value to avoid errors:
<code>@Value("${pack.name:}")
private String name;</code>Alternatively, configure a PropertySourcesPlaceholderConfigurer bean to ignore unresolved placeholders:
<code>@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
return configurer;
}</code>3. Custom Property Source
You can add a custom property source to the Spring Environment :
<code>public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> source = new HashMap<>();
source.put("pack.name", "中国🇨🇳");
PropertySource<?> propertySource = new MapPropertySource("pack", source);
environment.getPropertySources().addLast(propertySource);
}
}</code>Register the processor in META-INF/spring.factories :
<code>org.springframework.boot.env.EnvironmentPostProcessor=\
com.pack.PackEnvironmentPostProcessor</code>4. Enforce Required Properties
Mark properties that must be present by setting required properties in the environment:
<code>public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.setRequiredProperties("pack.version");
}
}</code>If pack.version is missing, the application will fail to start.
5. YML File Parsing
Spring Boot provides utilities to load custom YML files as different property sources:
<code>// Load as PropertySource
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
List<PropertySource<?>> sources = loader.load("pack", new ClassPathResource("pack.yml"));
// Load as Map
YamlMapFactoryBean mapFactory = new YamlMapFactoryBean();
mapFactory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml"));
// Load as Properties
YamlPropertiesFactoryBean propertiesFactory = new YamlPropertiesFactoryBean();
propertiesFactory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml"));</code>These five tips help you manage configuration properties more safely and flexibly in Spring Boot applications.
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.