最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring - What is the correct way to control the order in which @PropertySource annotations are applied? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Mar 14 at 17:06 vab2048vab2048 1,2672 gold badges13 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论