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());
}
}
}
}