My problem: I would like to contribute properties to the environment. I could do this using an EnvironmentPostProcessor
but I would like to do it using @PropertySource
because it is much more convenient.
However, there does not seem to be a way to describe the order in which a property source should be applied. I have tried putting them on @AutoConfiguration
classes and using @AutoConfigureBefore
and @AutoConfigureAfter
. I have tried putting them on normal @Configuration
classes and importing the @Configuration
from my auto-configuration. But none of these produce a predicatable order.
Also the code itself in spring-core
' adds the first property source it sees using addLast
and then adds each susequent one before that. See here with image screenshot of relevant part below:
This ends up in some weird reverse order from the first which is seen which confuses me even more.
Could you please advise on how to achieve ordering when using @PropertySource
on AutoConfigurations?
According to current spring (6.2.4
) javadoc there say:
In certain situations, it may not be possible or practical to tightly control property source ordering when using @PropertySource annotations. For example, if the @Configuration classes above were registered via component-scanning, the ordering is difficult to predict. In such cases — and if overriding is important — it is recommended that the user fall back to using the programmatic PropertySource API. See ConfigurableEnvironment and MutablePropertySources javadocs for details.
Within MutablePropreySources
one can use addAfter
, addBefore
, addFirst
, and addLast
methods in order to determine respective order of the property sources.
Unfortunately there does not seem to be an easy way to achieve respective ordering like implementing Ordered
interface.