Loading published curriculum…

12 · Spring Boot and the web request lifecycle / LESSON 52 OF 102

Typed configuration properties and validation

Treat configuration as typed input with validation instead of scattered string lookups.

Before this lesson: Starters, auto-configuration, and condition reports

Understand the topic

@ConfigurationProperties binds a related group of settings. Register the properties type through scanning or @EnableConfigurationProperties. Use Duration and other suitable types instead of parsing strings throughout services.

Combine @Validated with Jakarta validation constraints when the validation dependency is present. Fail startup for invalid mandatory settings. Keep secrets in deployment configuration and never put them in toString output or public actuator responses.

EXAMPLE · JAVA

StudyProperties.java

import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import jakarta.validation.constraints.NotNull;
@Validated
@ConfigurationProperties("study")
public record StudyProperties(@NotNull Duration reviewDelay) {}

What to expect

With @ConfigurationPropertiesScan and validation configured, study.review-delay=3d binds to a Duration; missing required input fails validation. Add a positive-duration rule for the full exercise.

YOUR TURN

Put it into practice

  1. 01Bind review delay and page-size limits into one properties type.
  2. 02Add validation for missing, negative, and excessively large values.
  3. 03Verify: Invalid configuration prevents startup with a useful property-specific message.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Treat configuration as typed input with validation instead of scattered string lookups.

  1. 01@ConfigurationProperties binds a related group of settings. Register the properties type through scanning or @EnableConfigurationProperties. Use Duration and other suitable types instead of parsing strings throughout services.
  2. 02Combine @Validated with Jakarta validation constraints when the validation dependency is present. Fail startup for invalid mandatory settings. Keep secrets in deployment configuration and never put them in toString output or public actuator responses.
  3. 03Trace the example: With @ConfigurationPropertiesScan and validation configured, study.review-delay=3d binds to a Duration; missing required input fails validation. Add a positive-duration rule for the full exercise.

A mistake worth avoiding

The properties record is never injectable.

DEBUGGING ROOM

Something broke. Where do you look?

The properties record is never injectable.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does @ConfigurationProperties alone always register a bean?

Choose one answer

BUILD IT YOURSELF

Bind review delay and page-size limits into one properties type.

  1. 1.Bind review delay and page-size limits into one properties type.
  2. 2.Add validation for missing, negative, and excessively large values.
  3. 3.Reproduce this failure, then repair it: The properties record is never injectable.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does @ConfigurationProperties alone always register a bean?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Invalid configuration prevents startup with a useful property-specific message.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references