0

Does Spring put all properties into the same hashmap/list?
How can I get a list of all of the properties being managed by Spring

For example, if I have two properties files and they define overlapping properties which

// FILE: src/main/resources/file1.properties
prop1.color = red
prop1.fill  = green
// FILE: src/main/resources/file2.properties
prop2.color = purple
prop1.fill  = ORANGE

The configuration class used to tell Spring to read the two files is something like this:

@PropertySource("classpath:file1.properties")
@PropertySource("classpath:file2.properties")
@Configuration
public class MyConfig {
}

My question is Can I get an iterator to access all of the properties defined? If so how.

Searching for answer

So far I haven't found a solution to this question.

I did an Internet search for "stack overflow Spring properties" and did not found the answer.

Some SO questions I found are:

But they didn't answer my question

PatS
  • 8,833
  • 12
  • 57
  • 100

1 Answers1

2

Spring provides a PropertySources interface which is implemented by the MutablePropertySources class. This class basically just holds a list of PropertySource instances (e.g. instances of the PropertiesPropertySource class - not to be confused with the @PropertySource annotation) and provides methods for iterating over these instances.

If you look at the implementation of MutablePropertySources you can see that there are multiple methods for adding PropertySource instances at different positions and relative to other PropertySources in that list. If you want to get the value of a property, you can call the get method of a MutablePropertySources instance with the name of that property. The get method then iterates over all PropertySources in its list and tries to find a property with the given name.

Then there is the Environment interface that is implemented by the abstract class AbstractEnvironment. You can use the latter to access the PropertySources instance that is used by that environment.

For example, for testing purposes, you can do the following to print out all properties and their values that are managed by MapPropertySources - a subtype of PropertySource.

@PropertySource("classpath:file1.properties")
@PropertySource("classpath:file2.properties")
@Configuration
public class ExampleConfig {

    @Autowired
    public ExampleConfig(Environment environment) {
        AbstractEnvironment env = (AbstractEnvironment) environment;
        for (org.springframework.core.env.PropertySource<?> source : env.getPropertySources()) {
            if (source instanceof MapPropertySource mapPropertySource) {
                System.out.println(mapPropertySource.getSource());
            }
        }
    }
}

You can check out the org.springframework.core.env package which contains all of these classes.

So, to answer your first answer more explicitly: No, Spring does not put all properties into a single map. It puts properties from different locations into different PropertySources which are then added to a PropertySources instance.

Alex R
  • 3,139
  • 1
  • 18
  • 28
  • Your answer (after I ran the code) also answered another question, namely: *Does Spring keep track of what file was used to define a property?* The answer is **yes**. Each property source has a separate entry in the list. My belief (not yet tested) is that if a property is defined in multiple files, the first file found "wins". – PatS Dec 28 '22 at 01:49