0 day streak
java / beginner
Snippet

Injecting Configuration with @Value

The @Value annotation allows you to inject simple values (like strings or integers) from external configuration files like 'application.properties' directly into your fields.

snippet.java
java
1
2
3
4
5
6
7
8
9
@Component
public class WelcomeMessage {
@Value("${app.welcome.text}")
private String message;
public String getMessage() {
return message;
}
}
spring
Breakdown
1
@Value("${app.welcome.text}")
Uses SpEL (Spring Expression Language) to look up a property key.
2
private String message;
The field where the configuration value will be stored.