Spring Boot Configuration Properties
@ConfigurationProperties is a powerful Spring Boot feature used to bind external configuration values (from properties or YAML files) to strongly-typed Java objects.
1. What is @ConfigurationProperties?
@ConfigurationProperties allows you to map a group of related configuration properties to a Java class in a clean, type-safe, and structured way.
Instead of injecting individual values using @Value,
@ConfigurationProperties binds the entire configuration section at once.
2. Why Use @ConfigurationProperties?
- Cleaner and more readable configuration code
- Type safety (compile-time checks)
- Easy maintenance for large configurations
- Supports validation
- Ideal for real-world, enterprise Spring Boot projects
3. @Value vs @ConfigurationProperties
| Aspect | @Value | @ConfigurationProperties |
|---|---|---|
| Usage | Single property | Group of properties |
| Type Safety | Low | High |
| Validation Support | No | Yes |
| Best For | Small configs | Large & complex configs |
4. Basic Example
application.properties
app.name=MySpringApp
app.version=1.0
app.owner=Sandeep
Configuration Class
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String version;
private String owner;
// getters and setters
}
Spring Boot automatically binds all properties starting with app to this class.
5. Using @EnableConfigurationProperties
If the configuration class is not annotated with @Component,
you must enable it explicitly.
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig {
}
6. Nested Properties (Very Important)
application.properties
db.url=jdbc:mysql://localhost:3306/mydb
db.username=root
db.password=secret
db.pool.max-size=20
Java Configuration
@Component
@ConfigurationProperties(prefix = "db")
public class DatabaseProperties {
private String url;
private String username;
private String password;
private Pool pool;
public static class Pool {
private int maxSize;
// getters and setters
}
// getters and setters
}
Nested objects help represent complex configurations clearly.
7. Using YAML with @ConfigurationProperties
application.yml
app:
name: MySpringApp
version: 1.0
owner: Sandeep
db:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
pool:
max-size: 20
YAML is highly recommended for large configuration structures.
8. Validation with @ConfigurationProperties
Spring Boot supports validation using javax.validation annotations.
@Component
@ConfigurationProperties(prefix = "db")
@Validated
public class DatabaseProperties {
@NotBlank
private String url;
@NotBlank
private String username;
@Min(5)
private int poolSize;
// getters and setters
}
If validation fails, the application fails to start – preventing bad configuration.
9. Immutable Configuration Properties (Best Practice)
Using constructor binding makes configuration classes immutable.
@ConfigurationProperties(prefix = "app")
@ConstructorBinding
public class AppProperties {
private final String name;
private final String version;
public AppProperties(String name, String version) {
this.name = name;
this.version = version;
}
// getters only
}
✔ Safer & cleaner design ✔ Recommended for senior-level projects
10. Using Configuration Properties in Services
@Service
@RequiredArgsConstructor
public class AppService {
private final AppProperties appProperties;
public void printDetails() {
System.out.println(appProperties.getName());
}
}
11. Common Use Cases
- Database configurations
- External API settings
- Security-related values
- Feature flags
- Cloud and microservice configuration
12. Common Mistakes
- Using @Value for large configs
- Forgetting getters/setters
- Missing @Component or @EnableConfigurationProperties
- Hard-coding sensitive values
13. Interview Questions (Must Know)
- Why use @ConfigurationProperties? Type safety and maintainability
- Difference between @Value and @ConfigurationProperties? Single vs grouped binding
- Does it support validation? Yes
- Can it be immutable? Yes, using constructor binding
14. Real-World Analogy
- @Value → Reading one item from a form
- @ConfigurationProperties → Reading the entire form into an object
Summary
@ConfigurationProperties is the recommended way to manage external configuration in Spring Boot applications. It keeps code clean, safe, scalable, and production-ready.