java / beginner
Snippet
Type-Safe Configuration Mapping
Instead of injecting individual values, @ConfigurationProperties allows you to map groups of external properties (from application.properties) into a structured Java object in a type-safe way.
snippet.java
1
2
3
4
5
6
7
8
9
10
@Component@ConfigurationProperties(prefix = "app.settings")public class AppSettings {private String theme;private int maxUsers;// Getters and setters are required for bindingpublic String getTheme() { return theme; }public void setTheme(String theme) { this.theme = theme; }}
spring
Breakdown
1
@ConfigurationProperties(prefix = "app.settings")
Maps keys starting with 'app.settings' (e.g., app.settings.theme) to the class fields.
2
private String theme;
The field name must match the property key suffix for automatic binding.