@Value | @ConfigurationProperties | |
|---|---|---|
| Binding | 1 property | Nhóm properties vào class |
| Type-safe | Hạn chế (SpEL) | ✅ Full type conversion |
| Validation | Không | ✅ @Validated + Bean Validation |
| IDE support | Kém | ✅ autocomplete, navigation |
java
// @Value — 1 property
@Value("${server.port:8080}")
private int port;
// @ConfigurationProperties — nhóm property (RECOMMENDED)
@ConfigurationProperties(prefix = "app.mail")
@Validated
public record MailProps(
@NotBlank String host,
@Min(1) @Max(65535) int port,
boolean ssl
) {}Enable:
java
@SpringBootApplication
@ConfigurationPropertiesScanKhi dùng @Value: 1 property đơn lẻ, SpEL expression phức tạp.
Khi dùng @ConfigurationProperties: config có cấu trúc (>=3 key liên quan), cần validation, viết test.
@Value | @ConfigurationProperties | |
|---|---|---|
| Binding | Single property | Group of properties in a class |
| Type-safe | Limited (SpEL) | ✅ Full type conversion |
| Validation | No | ✅ @Validated + Bean Validation |
| IDE support | Poor | ✅ autocomplete, navigation |
java
// @Value — single property
@Value("${server.port:8080}")
private int port;
// @ConfigurationProperties — grouped properties (RECOMMENDED)
@ConfigurationProperties(prefix = "app.mail")
@Validated
public record MailProps(
@NotBlank String host,
@Min(1) @Max(65535) int port,
boolean ssl
) {}Enable:
java
@SpringBootApplication
@ConfigurationPropertiesScanUse @Value: a single isolated property, complex SpEL expressions.
Use @ConfigurationProperties: structured config (3+ related keys), validation needed, tests.