Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Spring Boot configuration properties: bind values and reject bad startup input

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

ConfigurationProperties binds a named property prefix to a typed object, and validation constraints can reject invalid configuration at the binding boundary.

Download Spring source kit

This lesson uses the downloadable source kit: Java 21, Spring Boot 4.0.8 and its managed Spring Framework 7 dependencies. The version is pinned for repeatable builds.

Parse into a complete configuration

ImportConfiguration binds batch size and timeout under receipt.import. The record requires a batch between one and five hundred and a non-null Duration. The kit uses 32 and 5s, then tests invalid zero directly with the validator.

The Binder test confirms type conversion, and the validation test confirms the declared bounds. Those separate checks do not alone prove integrated startup validation; the configuration is also registered by EnableConfigurationProperties in the running application.

A value is not a secret store

Configuration source precedence can override a packaged default. Store production credentials outside the source kit and avoid logging the bound object when it contains secrets. A command-line override is visible in process arguments on many systems.

A timeout value needs meaning at the operation that consumes it. Binding five seconds does not make every JDBC, HTTP or shutdown operation obey that budget. Configuration validation in Java explains why parsing and application-state changes should be separate steps.

Checked source

Java
package in.aitrove.learning;
import java.time.Duration;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@Validated
@ConfigurationProperties("receipt.import")
public record ImportConfiguration(@Min(1) @Max(500) int batchSize, @NotNull Duration timeout) {}

Test the boundary

Run mvn test in the source-kit directory. CoreBoundaryTest.propertyBinderMapsDuration and validationRejectsOutOfRangeBatch checks the behavior described here. Java excerpts belong to the named source-kit classes; they are not independent source files unless the complete class is shown.

Costs and boundaries

Binding happens during assembly in this application. A bounded batch size caps one dimension of retained work; payload bytes and concurrent batches still need their own limits.

Common Mistakes

  • Do not confuse successful parsing with validated ranges.
  • Do not log secrets by dumping configuration records.
  • A bound timeout has no effect until an operation uses it.

Read next

Profiles properties, Java Properties: validate configuration before changing application state, Java daylight-saving transitions: reject gaps and choose overlaps.

Extend this boundary

Continue with Java DecimalFormat: reject trailing input and pin the locale.

Next boundary checks

Continue with Spring Boot property override: fail startup on the effective value.

Continue with checked settings and schema rollout

Continue with Spring Boot Duration binding: make timeout units explicit, Spring Boot configuration validation: reject an unusable relay before work starts.

Checked file and migration continuation

Continue with Spring Boot config validation: separate valid syntax from a safe relay setting.

spring
spring-boot
configuration-properties
Storage details