Author
@ConfigurationProperties is used in Spring Boot to bind external configuration values (from application.properties or application.yml) to a typed Java class.
Instead of reading properties one by one using @Value, this annotation allows you to map related properties into a structured object, making configuration clean, safe, and maintainable.
@Value annotationsapplication.yml
app:
name: My Spring App
version: 1.0
security:
enabled: true
Configuration Class
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String version;
private Security security;
public static class Security {
private boolean enabled;
// getters and setters
}
// getters and setters
}
prefix = "app" maps all properties starting with appapp.name → nameapp.security.enabled → security.enabledSpring Boot automatically binds values at startup.
If you don’t want to annotate the class with @Component:
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig {
}
Spring Boot provides two common ways to read values from
application.properties or application.yml:
@Value → Best for single or simple values@ConfigurationProperties → Best for grouped and complex configurationsUnderstanding the difference helps you write cleaner, scalable, and maintainable code.
@ConfigurationProperties
Example:
app:
name: MyApp
timeout: 30
enabled: true
All these values are bound to one config class.
@Value
@ConfigurationProperties
Example:
private int timeout;
private boolean enabled;
@Value
@ConfigurationProperties
@Value
@ConfigurationProperties
@Value
@ConfigurationProperties
@Value
| Aspect | @ConfigurationProperties | @Value |
|---|---|---|
| Property Grouping | Groups related properties in one class | Reads single property at a time |
| Type Safety | Strong, validated at startup | Limited, errors at runtime |
| YAML Support | Excellent (nested, lists, maps) | Poor for complex YAML |
| Scalability | Best for large & enterprise apps | Suitable only for small configs |
| Maintainability | Clean, structured, easy to manage | Noisy and hard to maintain |
| Profiles Support | Works smoothly with dev/test/prod | Less practical with many profiles |
You can validate configuration values using @Validated.
@Component
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
@NotBlank
private String name;
@Min(1)
private int timeout;
// getters and setters
}
If validation fails, application startup will fail, preventing bad configuration.
dev, test, prod)@ConfigurationProperties over @Value for multiple values@ConfigurationProperties maps configuration files to Java objects