Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through all the properties in a file, with spring and java

Normally i would populate a field using annotations when I knew the property name like so :

@Value("${myproperties.myValue}")
private String myString

However I now want to loop through all the properties in a file, when their names are unknown, and store both there value and name. What's the best way with spring and java ?

like image 749
NimChimpsky Avatar asked Jan 03 '13 08:01

NimChimpsky


People also ask

How read multiple values from properties in spring boot?

A Singleton in Spring Boot This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application. properties is not used because it is implicitly included in the PropertySource by Spring Boot.

How does Spring know which properties file to use?

When it comes to property files it checks for application. properties and then application-<active_profile>. properties where <active_profile> is set by spring.

How read data from properties file in Java?

Each key and its corresponding value in the property list is a string. The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load .


1 Answers

Actually if you need only to read properties from a file and not to use these properties in Spring's property placeholders, then the solution is simple

public class Test1 {
    @Autowired
    Properties props;

    public void printProps() {
        for(Entry<Object, Object> e : props.entrySet()) {
            System.out.println(e);
        }
    }

...

<util:properties id="props" location="/spring.properties" />
like image 200
Evgeniy Dorofeev Avatar answered Oct 13 '22 21:10

Evgeniy Dorofeev