📦
Configuration & Properties

@ConfigurationProperties

Type-Safe Configuration
💡 @ConfigurationProperties ek FORM TEMPLATE hai — @Value se EK-EK VALUE MANUALLY MANGWANE ke bajaye (jaise EK-EK FIELD ka SEPARATE FORM), EK POORA STRUCTURED FORM (Java class) DEFINE karo, aur SAARI RELATED SETTINGS AUTOMATICALLY, TYPE-SAFE tareeke se, us FORM mein BHAR jaati hain.

@ConfigurationProperties(prefix = "app.mail") ek CLASS ko MARK karta hai — is PREFIX ke SAARE properties (app.mail.host, app.mail.port, app.mail.username) AUTOMATICALLY is CLASS ke MATCHING FIELDS mein BIND ho jaate hain. @Value se COMPARE karo — @Value EK-EK PROPERTY ke liye SEPARATE ANNOTATION mangता hai, @ConfigurationProperties EK BAAR mein SAARI RELATED PROPERTIES GROUP karta hai.

SABSE BADA fayda — TYPE SAFETY. String property (jaise "8080") AUTOMATICALLY int FIELD mein CONVERT ho jaati hai. Agar CONVERSION FAIL ho (jaise "abc" ko int mein), STARTUP par hi ERROR AATA hai — "FAIL FAST" principle, RUNTIME mein SURPRISE avoid karta hai.

@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
    private String host;
    private int port;
    private boolean tlsEnabled;
    // getters, setters
}

// application.properties:
// app.mail.host=smtp.gmail.com
// app.mail.port=587
// app.mail.tls-enabled=true

// Enable karna (main class ya @Configuration par):
@SpringBootApplication
@EnableConfigurationProperties(MailProperties.class)
public class MyApp { }

// Usage — automatically POORI CLASS inject ho jaati hai:
@Service
public class EmailService {
    private final MailProperties mailProperties;
    public EmailService(MailProperties mailProperties) {
        this.mailProperties = mailProperties;
    }
}
📦
@ConfigurationProperties ek FORM TEMPLATE hai — @Value se EK-EK VALUE MANUALLY MANGWANE ke bajaye (jaise EK-EK FIELD ka SEPARATE FORM), EK POORA STRUCTURED FORM (Java class) DEFINE karo, aur SAARI RELATED SETTINGS AUTOMATICALLY, TYPE-SAFE tareeke se, us FORM mein BHAR jaati hain.
1 / 6
⚡ Quick Recap
  • @ConfigurationProperties(prefix) = related properties EK CLASS mein GROUP
  • @Value se BEHTAR — TYPE-SAFE, VALIDATION-friendly, LESS boilerplate
  • String → int/boolean automatic CONVERSION, FAIL FAST agar INVALID
Is page mein (2 subtopics)

@ConfigurationProperties classes ko Bean Validation annotations (@NotBlank, @Min, @Max) se COMBINE kiya ja sakta hai — @Validated annotation lagाके, agar CONFIGURATION values INVALID hain, application STARTUP par hi FAIL ho jaayegi ("fail fast"), RUNTIME mein bad config se BACHATA hai.

@ConfigurationProperties(prefix = "app.mail")
@Validated
public class MailProperties {
    @NotBlank
    private String host;

    @Min(1) @Max(65535)
    private int port;
}

@ConfigurationProperties classes ke ANDAR, NESTED objects bhi ho sakte hain — COMPLEX, HIERARCHICAL configuration ko TYPE-SAFE tareeke se REPRESENT karne ke liye, jaise app.security.jwt.* aur app.security.cors.* ALAG-ALAG NESTED classes mein.

@ConfigurationProperties(prefix = "app.security")
public class SecurityProperties {
    private Jwt jwt = new Jwt();
    private Cors cors = new Cors();

    public static class Jwt {
        private String secret;
        private long expiration;
    }
    public static class Cors {
        private List<String> allowedOrigins;
    }
}