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
1
2
3
4
5
6
7
8
9
@Componentpublic 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.