0

I have a singleton configuration class where I want to store all the properties for our web application.

How do we read in the application.properies file like any other properties file without using annotations?

What is the the fully qualified filename for application.properies i.e. /application.properies?

We only want to read application.properties once.

Albert
  • 11
  • 3
  • 1
    Sounds like you're trying to fight against the framework. Spring boot can read configuration properties from multiple sources (eg. environment variables, Spring cloud config, profile-specific `application.properties`, arbitrary locations of properties, ...). If you're going to manually read certain file, you're probably going to lose this feature. – g00glen00b Feb 05 '19 at 10:27

1 Answers1

0

Spring boot already reads all the properties stored in application.properties and much more, read Externalized Configuration documentation.

If you want to map one property named server.port you can just use @Value("${server.port}") Integer port.

If you want to access to all the properties loaded by Spring Boot, you can use the Environment object and access to all loaded PropertySources and retrieve all values from each property source.

In this this answer shows how. However, to avoid losing the precedence order of loaded properties, you have to reverse the property source list. Here you can find the code to load all the properties without losing the spring precedence order:

@Configuration
public class AppConfiguration {
    @Autowired
    Environment env;

    public void loadProperties() {
        Map<String, Object> map = new HashMap();

        for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator().reverse(); it.hasNext(); ) {
            PropertySource propertySource = (PropertySource) it.next();
            if (propertySource instanceof MapPropertySource) {
                map.putAll(((MapPropertySource) propertySource).getSource());
            }
        }
    }
}
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123