0 day streak
java / intermediate
Snippet

Type-safe Configuration with @ConfigurationProperties

@ConfigurationProperties allows you to map entire blocks of application.properties or application.yml files into a POJO. This provides type safety and IDE autocompletion for your configuration settings.

snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
@ConfigurationProperties(prefix = "app.storage")
public class StorageProperties {
private String location;
private int maxFileSize;
// Getters and Setters are required
public String getLocation() { return location; }
public void setLocation(String location) { this.location = location; }
public int getMaxFileSize() { return maxFileSize; }
public void setMaxFileSize(int maxFileSize) { this.maxFileSize = maxFileSize; }
}
spring
Breakdown
1
prefix = "app.storage"
Tells Spring to look for properties starting with 'app.storage' in the config files.
2
private int maxFileSize
The property 'app.storage.max-file-size' will be automatically converted to an integer.
3
setters are required
Spring uses standard setter injection to populate the values from the environment.