📘 Premium Read: Access my best content on
Medium member-only articles
— deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount):
My Udemy Courses - Ramesh Fadatare
— All my Udemy courses are real-time and project oriented courses.
🚀 Introduction: What is @ConfigurationProperties in Spring Boot?
The @ConfigurationProperties annotation in Spring Boot is used to map external configuration properties (from application.yml or application.properties) to Java objects. It helps in keeping configuration type-safe, structured, and reusable.
✅ Key Features of @ConfigurationProperties: ✔ Binds external properties (application.yml, application.properties) to Java classes. ✔ Supports prefix-based mapping for structured configuration. ✔ Works with YAML and properties files. ✔ Allows validation using @Validated.
📌 In this guide, you’ll learn: ✅ How @ConfigurationProperties works in Spring Boot. ✅ How to use it with YAML/properties files. ✅ How to validate configuration values.
1️⃣ Why Use @ConfigurationProperties Instead of @Value?
📌 Use @ConfigurationProperties when working with structured, reusable configurations. 📌 Use @Value for injecting a single property.
2️⃣ Setting Up @ConfigurationProperties in Spring Boot
1. Enable Configuration Properties in the Main Class
@SpringBootApplication @EnableConfigurationProperties(AppConfig.class) // Enable configuration binding public class SpringBootApp { publicstaticvoidmain(String[] args) { SpringApplication.run(SpringBootApp.class, args); } }
✅ @EnableConfigurationProperties allows Spring Boot to scan configuration classes.
2. Define a Configuration Class (AppConfig.java)
@ConfigurationProperties(prefix = "app")// Maps properties with 'app' prefix publicclassAppConfig {
private String name; private String version;
// Getters and Setters }
✅ Maps app.name and app.version from the properties file.
3. Define Properties in application.yml
app: name: "MyApp" version: "1.0.0"
✅ Spring Boot automatically binds these values to AppConfig.
public ServerController(ServerConfig serverConfig) { this.serverConfig = serverConfig; }
@GetMapping("/server-info") public String getServerInfo() { return"Hosts: " + serverConfig.getHosts() + ", DB URL: " + serverConfig.getDatabase().getUrl(); } }
📌 GET Request (GET /api/server-info)
Response: Hosts: [host1.example.com, host2.example.com], DB URL: jdbc:mysql://localhost:3306/mydb
✅ Spring Boot successfully maps nested configurations.
4️⃣ Validating Configuration Properties
Spring Boot allows automatic validation of configuration properties using @Validated.
1. Enable Validation in the Configuration Class
@ConfigurationProperties(prefix = "app") @Validated public class AppConfig {
@NotBlank// Ensures name is not blank private String name;
@Pattern(regexp = "\\d+\\.\\d+\\.\\d+") // Ensures version follows X.Y.Z format private String version;
// Getters and Setters }
✅ If app.name is blank or app.version is invalid, the application fails to start.
5️⃣ Using @ConstructorBinding for Immutable Configurations
From Spring Boot 2.2+, you can use @ConstructorBinding to make configuration properties immutable (no setters required).
📌 Example: Immutable Configuration Class
@ConfigurationProperties(prefix = "app") @ConstructorBinding public record AppConfig(String name, String version) {}
✅ No need for setters — values are set at the time of object creation.
🎯 Summary: Best Practices for Using @ConfigurationProperties
✅ Use @ConfigurationProperties for structured configurations. ✅ Use @Validated to enforce validation rules. ✅ Use @ConstructorBinding for immutable configurations. ✅ Group related properties in a single class instead of multiple @Value injections. ✅ Keep property names consistent and meaningful.
🚀 Following these best practices ensures clean, maintainable configurations in Spring Boot!
Related Spring Boot and Microservices Tutorials/Guides:
Comments
Post a Comment
Leave Comment