0 day streak
java / beginner
Snippet

Type-Safe Configuration with @ConfigurationProperties

Instead of multiple @Value annotations, @ConfigurationProperties allows you to map a group of related properties (e.g., mail.host, mail.port) directly to a POJO.

snippet.java
java
1
2
3
4
5
6
7
8
9
@Configuration
@ConfigurationProperties(prefix = "mail")
public class MailConfig {
private String host;
private int port;
public void setHost(String host) { this.host = host; }
public void setPort(int port) { this.port = port; }
}
spring
Breakdown
1
@ConfigurationProperties(prefix = "mail")
Binds all properties starting with 'mail' to this class fields.
2
public void setHost(String host)
Setters are required for Spring to inject the values into the fields.