Priority of Various Sources in PropertySources

无人久伴 提交于 2021-01-21 04:17:24

问题


Spring has introduced a new annotation @PropertySources for all classes marked as @Configuration since 4.0. It takes different @PropertySource as argument.

@PropertySources({
    @PropertySource("classpath:application.properties"), @PropertySource("file:/tmp/application.properties")})

What I am interested is knowing is the ordering in case of conflict in values for the same key present in multiple properties file. I have not seen any documentation related to this that specifies an ordering. I have tries it multiple times and found that the PropertySource mentioned later is overwriting the value present in PropertySource mentioned before. But, how to be sure?


回答1:


The documentation of @PropertySources does not say anything about the case where the same property exists in more than one @PropertySource files.

However, the documentation of @PropertySource states the following :

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will 'win' and override

Since the @PropertySource declarations within @PropertySources are actually a table, then it is fairly safe to assume that the last declared @PropertySource overrides the previous ones. This is consistent with the tests I've done and with this blog post.

However, as mentioned in the question, it is not indicated clearly in the documentation. So the behavior might "accidentally" change in the future.




回答2:


It seems likely that @PropertySource annotations inside of a @PropertySources annotation are processed in the order they appear, based on my tests, however if you set up a listener, they will print in reverse order.

Annotations:

@Configuration
@PropertySources({
  @PropertySource(value = "classpath:application.global.properties"),
  @PropertySource(
      value = "classpath:application.client-specific.properties",
      ignoreResourceNotFound = true),
  @PropertySource(
      value = "file:/etc/omnia/application.client-specific.properties",
      ignoreResourceNotFound = true),
  @PropertySource(value = "classpath:application.test.properties", ignoreResourceNotFound = true)
})

And the logging output:

Loading @PropertySource: 'configurationProperties'
Loading @PropertySource: 'servletConfigInitParams'
Loading @PropertySource: 'servletContextInitParams'
Loading @PropertySource: 'systemProperties'
Loading @PropertySource: 'systemEnvironment'
Loading @PropertySource: 'random'
Loading @PropertySource: 'URL [file:/etc/omnia/application.client-specific.properties]'
Loading @PropertySource: 'class path resource [application.global.properties]'



回答3:


HL'REB is right. "Win" the parameter from the last properties file. However, application.properties overrides all values. Checked for SPRING 5.1.6.



来源:https://stackoverflow.com/questions/32251351/priority-of-various-sources-in-propertysources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!