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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
5 Essential Spring Boot 3 Techniques for Validating and Customizing Configuration Properties

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.

@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;
}

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.

@Value("${pack.name}")
private String name;

Provide a default value to avoid errors:

@Value("${pack.name:}")
private String name;

Alternatively, configure a PropertySourcesPlaceholderConfigurer bean to ignore unresolved placeholders:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    return configurer;
}

3. Custom Property Source

You can add a custom property source to the Spring Environment:

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);
    }
}

Register the processor in META-INF/spring.factories:

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

4. Enforce Required Properties

Mark properties that must be present by setting required properties in the environment:

public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        environment.setRequiredProperties("pack.version");
    }
}

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:

// 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"));

These five tips help you manage configuration properties more safely and flexibly in Spring Boot applications.

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.

javaSpring BootYAMLconfiguration-propertiesEnvironmentPostProcessor
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.