How to use ConfigurationProperties with Records?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@ConfigurationProperties works well with Java records because records give you immutable, concise configuration holders with almost no boilerplate. The main idea is to bind external configuration into a record whose components represent the properties under a common prefix. In modern Spring Boot, this is a clean alternative to mutable setter-based configuration beans.
A Basic Record Configuration Class
Suppose you have properties like these:
You can bind them into a record:
The record components map naturally to configuration keys. Kebab-case properties bind to camelCase record components.
Enable Configuration Properties Scanning
Spring needs to discover the class. The common approach is enabling configuration properties scanning on your application.
With scanning enabled, Spring can create the record-based bean automatically.
Inject the Record Like Any Other Bean
Once registered, the record can be injected into services or configuration classes.
Notice that record accessors are named after the components themselves, not getBaseUrl().
Nested Records Work Well Too
Complex configuration structures can be modeled with nested records.
Matching YAML:
This keeps hierarchical configuration strongly typed and immutable.
Validation Still Applies
You can validate record-based configuration just as you would a regular properties class.
If a property is missing or invalid, application startup fails early, which is usually exactly what you want for required configuration.
Version Considerations
Record support depends on using a Spring Boot version new enough to handle constructor-style binding cleanly. In modern Spring Boot, records work naturally because binding happens through the canonical constructor. In older Boot versions, you may see examples with @ConstructorBinding.
The practical rule is simple:
- modern Spring Boot: records work cleanly with constructor binding semantics
- older projects: verify whether explicit constructor binding annotations are still required
That version detail matters when migrating legacy code.
When Records Are a Good Fit
Records are especially good when configuration should be:
- immutable after startup
- simple and declarative
- grouped by prefix
- validated at startup
They are less attractive when you need unusual custom binding behavior or a lot of imperative configuration logic inside the properties class.
Common Pitfalls
The biggest mistake is forgetting to register the properties class through scanning or explicit enablement. The record then looks correct but never becomes a bean.
Another issue is using property names that do not map cleanly to the record components and then assuming binding is broken. Check the prefix and exact property names carefully.
Developers also sometimes write getter-style access like getBaseUrl(). Record components use accessor methods named exactly after the component.
Summary
- '
@ConfigurationPropertiesand Java records are a good match for immutable config.' - Use a record with a clear prefix and enable configuration properties scanning.
- Inject the record like any other Spring bean.
- Nested records work well for hierarchical settings.
- Add validation so invalid configuration fails fast at startup.
Related reading
- How to use Firebase with Spring boot REST Application?
- How to use HikariCP in Spring Boot with two datasources in conjunction with Flyway
- How to use Jackson to deserialise an array of objects
- How to use Java property files?
- How to use java.String.format in Scala?
- How to use JUnit to test asynchronous processes
- How to use JUnit to test asynchronous processes
- How to use LocalDateTime RequestParam in Spring? I get Failed to convert String to LocalDateTime

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.