What is @ConfigurationProperties and how does it bind external configuration?
How @ConfigurationProperties binds external config in Spring Boot: prefix mapping, relaxed binding, nested types, validation, and constructor binding.
Expected Interview Answer
@ConfigurationProperties is a Spring Boot annotation that maps a group of external properties (from application.yml, application.properties, environment variables, or command-line args) onto the fields of a strongly typed Java bean by matching a common prefix.
Spring binds properties by relaxed naming, so app.max-retries, app.maxRetries, and APP_MAXRETRIES all map to the maxRetries field. You enable a properties class with @EnableConfigurationProperties or by annotating it with @ConfigurationProperties plus @Component, and constructor binding with @ConstructorBinding lets you make it immutable. It supports nested objects, lists, maps, type conversion, and JSR-303 validation via @Validated, giving type-safe, testable configuration instead of scattered @Value lookups.
- Groups related settings into one type-safe object
- Supports relaxed binding across naming styles and env vars
- Handles nested objects, lists, and maps automatically
- Enables validation with @Validated and JSR-303 constraints
- Supports immutable config via constructor binding
- Easier to test and refactor than scattered @Value annotations
AI Mentor Explanation
@ConfigurationProperties is like a team sheet form with fixed labeled fields — captain, wicketkeeper, twelfth man — that the coach fills from a big pool of paperwork. Instead of hunting each detail from loose notes every time, the labeled form gathers all settings under one heading and hands the side a clean, structured lineup ready to read at a glance.
Step-by-Step Explanation
Step 1
Define the prefix
Annotate a POJO with @ConfigurationProperties(prefix = "app") to claim all app.* keys.
Step 2
Add typed fields
Create fields (and nested objects, lists, maps) whose names match the property keys under relaxed binding.
Step 3
Register the bean
Use @EnableConfigurationProperties(AppProps.class) on a config class, or @Component on the properties class.
Step 4
Supply the values
Set them in application.yml/properties, environment variables, or command-line args — all bind by relaxed rules.
Step 5
Validate and inject
Add @Validated with constraints, then inject the properties bean wherever the settings are needed.
What Interviewer Expects
- Understanding prefix-based binding onto a typed bean
- Knowing relaxed binding across naming styles and env vars
- How to enable the bean via @EnableConfigurationProperties or @Component
- Support for nested objects, lists, and maps
- Validation with @Validated and JSR-303
- Why it is preferable to many scattered @Value annotations
Common Mistakes
- Forgetting to register the properties bean, so nothing binds
- Expecting exact key names instead of relaxed binding
- Missing setters when not using constructor binding
- Not adding @Validated when validation is expected
- Confusing @ConfigurationProperties with @Value for single keys
Best Answer (HR Friendly)
“@ConfigurationProperties lets you map a whole group of settings from config files or environment variables onto one neat Java object based on a shared prefix. It gives you type-safe, validated configuration instead of reading each setting one by one.”
Code Example
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
@NotBlank
private String name;
@Min(1)
private int maxRetries;
private List<String> allowedHosts;
// getters and setters (or use constructor binding)
}
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig { }
# application.yml
# app:
# name: skillveris
# max-retries: 3
# allowed-hosts: [a.com, b.com]Follow-up Questions
- How does @ConfigurationProperties differ from @Value?
- What is relaxed binding and which naming styles does it accept?
- How do you validate configuration properties?
- How does constructor binding create immutable config?
- How do environment variables map to nested properties?
MCQ Practice
1. What does @ConfigurationProperties bind onto a Java bean?
It maps all properties under a given prefix onto the matching fields of a typed bean, from any property source.
2. Which annotation registers a @ConfigurationProperties class as a bean?
@EnableConfigurationProperties (or annotating the class with @Component) registers the properties bean for binding.
3. Under relaxed binding, which key maps to a field named maxRetries?
Relaxed binding matches kebab-case, camelCase, and upper-case env-var forms to the same field.
Flash Cards
What does @ConfigurationProperties do? — Binds a group of prefixed external properties onto a typed Java bean.
How to register the bean? — @EnableConfigurationProperties(Props.class) or annotate the class with @Component.
What is relaxed binding? — Matching kebab-case, camelCase, and ENV_VAR forms of a key to the same field.
How to validate config? — Add @Validated and JSR-303 constraints like @NotBlank and @Min.
@ConfigurationProperties vs @Value? — The former binds grouped, type-safe config; @Value injects a single expression.
Continue Learning
Related Interview Questions
How do you supply configuration to a Spring Boot service running in containers, and how do changes take effect?
medium
The same property is set in three places — how does Spring Boot decide which value wins?
medium
What is auto-configuration in Spring Boot and how does it work?
medium
What is dependency injection in Spring and how does the IoC container work?
medium