how to read from property file in spring boot

dzoneuser

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

file:applicaton.properties

#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

Anthony BONNIER

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.

edited at
0

Comments

0 comments
Login to comment

Related

Multiple external property file in spring boot from command line

How to read data from java properties file using Spring Boot

How to read excel file using spring boot

spring boot read properties file from different maven module

@ServiceActivator How to read inputChannel from property file in spring boot?

Spring boot read array from YAML (properties) file

How to read a property on a static field in a spring-boot application?

How to get spring boot property file value as Map<String,String>

How to get property file in a Configuration class implementing BeanDefinitionRegistryPostProcessor in spring boot

spring - read property value from properties file in static field of class

Spring boot how to read properties file outside jar

How to read property file values into set of Strings in Spring boot

Spring Boot not reading from property file

How to load an external property file to Spring Boot in Wildfly

How to read data from csv file and store it in the database ? Spring Boot

Spring boot, how to read particular object from json file, what annotation do I need?

How to directly read application.property value into another configuration xml file in spring boot

How to read JSON file from resources in Spring Boot

read .html file from resources folder on server in spring boot

Read property from config server in spring boot 2.4

Spring: Read property from file why executing Condition::match

Accessing property file in spring boot

How to access a file property from Spring Condition?

How i can read any file from resources folder in java spring-boot AZURE function?

Unable to read file from spring boot .jar

How to read spring boot application property information inside the library

How to upload and read file in Spring Boot

How to read the application.properties file from a location outside the spring boot application

How to read a static file on Heroku deployment on Spring Boot