application.yml file
countries:['India','USA']
How to read this countries parameters in Java with @Value
annotation?
application.yml file
countries:['India','USA']
How to read this countries parameters in Java with @Value
annotation?
use comma separated values in application.yml
countries: India, USA
java code for access
@Value("${countries}")
String[] countries
First Approach:
Your YAML list should look like this:
acme:
countries:
- India
- USA
Then add the following annotation to your Country class:
@ConfigurationProperties(prefix = "acme")
public class Country {
private List<String> countries = new ArrayList<>();
//constructor, getter/setter etc.
}
Lastly you need to add another annotation to your SpringBootApplication app:
@SpringBootApplication
@EnableConfigurationProperties(Country.class)
Finally get the countries by autowiring the Country class and calling the getter-method.
Second Approach:
YAML:
countries: India, USA
Country.class
@Value("${countries}")
private List<String> countries;