Spring Boot FAQ: Top Questions
18. What is the use of @ConfigurationProperties in Spring Boot?
@ConfigurationProperties
is used to bind a group of related properties to a Java class, supporting structured configuration.
πΊοΈ Step-by-Step:
- Create a POJO with matching fields and annotate it with
@ConfigurationProperties
. - Enable it using
@EnableConfigurationProperties
or@Component
.
π₯ Example:
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
}
π Expected Output:
Maps app.name and app.version from configuration file.
π οΈ Use Cases:
- Cleanly managing grouped configuration properties.
- Making configuration reusable and injectable.