Andrés Monge Moreno opened SPR-10241 and commented

It would be nice if it was possible to access to all the properties pairs "key" -> "value" that are contained in the Environment when using the @PropertySource annotation. Mainly, adding one method getAllProperties(): Map<String,String> would be enough and would let a lot of flexibility in order to iterate over all the properties without exactly knowing their keys.


No further details from SPR-10241

Comment From: spring-projects-issues

Phil Webb commented

Unfortunately I don't think this will be possible. The PropertyResolver interface and PropertySource class are intentionally limited in this respect so that they can be backed by providers that might not have the ability to iterate keys.

The ConfigurableEnvironment interface does expose a getPropertySources() method that could be used to iterate the underlying PropertySource interfaces. Any EnumerablePropertySource can have property names inspected. Something like:

public static Map<String, Object> getAllKnownProperties(Environment env) {
    Map<String, Object> rtn = new HashMap<>();
    if (env instanceof ConfigurableEnvironment) {
        for (PropertySource<?> propertySource : ((ConfigurableEnvironment) env).getPropertySources()) {
            if (propertySource instanceof EnumerablePropertySource) {
                for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
                    rtn.put(key, propertySource.getProperty(key));
                }
            }
        }
    }
    return rtn;
}

Comment From: darkstone

This can be done using the following already available API:

Given the following object:

internal class AboutApi {
    lateinit var purpose: String
    lateinit var version: String
    lateinit var copyright: String
    lateinit var notice: String
    lateinit var contact: String
    lateinit var description: String
}

Could be extracted from the injected environment in the following manner:


 private fun about() : AboutApi {
        val binder = Binder.get(env)
        val target = Bindable.ofInstance(AboutApi())
        binder.bind("api.about", target)
        return target.value.get()
    }