java / beginner
Snippet
Reading Values from Properties
Use @Value to pull configuration data from your application.properties file. The part after the colon provides a default value if the property key is missing.
snippet.java
1
2
3
4
5
6
7
8
import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class WelcomeConfig {@Value("${app.welcome.text:Hello!}")private String message;}
spring
Breakdown
1
@Value("${app.welcome.text:Hello!}")
Looks for 'app.welcome.text' in properties; defaults to 'Hello!' if not found.