java / beginner
Snippet
Reading Typed Application Properties via Value Annotation
The @Value annotation injects configuration values from application.properties or application.yml into Spring bean fields. Spring automatically converts string property values into target primitive types like int or boolean and supports fallback default values using a colon separator.
snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class AppConfigHolder {@Value("${app.timeout:30}")private int connectionTimeout;@Value("${app.feature.enabled:false}")private boolean isFeatureEnabled;public int getConnectionTimeout() {return connectionTimeout;}}
spring
Breakdown
1
@Value("${app.timeout:30}")
Injects the property 'app.timeout' as an integer, defaulting to 30 if the key is not defined.
2
private int connectionTimeout;
Holds the injected primitive integer value after automatic type conversion.
3
@Value("${app.feature.enabled:false}")
Injects a boolean configuration flag, defaulting to false if omitted.