I am able to read (value of 'fruits') from property file in my spring boot application, successfully using @Value as below.
@Value("${fruits}")
private String[] fruitarray;
from the below
#section_1
fruits=apple,mango,banana
#section_2
apple.native=aaaa
apple.cost=100
apple.name=xxyyzz
Now, I would like to know, how can I access the key-values from the section_2 of the properties file dynamically. I mean,... in our above code we already got array of fruits and set it to 'fruitarray' using @Value. Now how would I be accessing value for 'apple.native' by using fruitarray[0] variable in java/spring-boot way?
Thanks
You could try to get all the properties as a Bean and access dynamically to your properties while looping over the fruits list :
@Autowired
private Environment env;
...
env.getProperty(fruits[i] + ".native");
...
Source : http://www.baeldung.com/properties-with-spring#usage
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments