Spring - 如何用外部文件覆盖内部配置文件 [英] Spring - how to override internal config file with external file

查看:78
本文介绍了Spring - 如何用外部文件覆盖内部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 spring 项目中,我有两个不同的 conf 文件,它们具有相同的属性.

In my spring project I'm having two different conf files which have the same properties.

其中一个在外部目录中,而另一个在我的项目资源中.

One of them is in an external directory while the other one is inside the resource of my project.

我希望外部文件的属性覆盖内部值(如果它们存在于外部文件中).

I want that the external file's properties to override the internal values (if they exist in the external file).

在我的配置类中,我使用 PropertySource 注释设置文件:

In my configuration class I set the files with the PropertySource annotation:

@PropertySources(
{ @PropertySource("file:${HOME}/conf/application.properties"),  
  @PropertySource("classpath:/data.properties")  
})

推荐答案

默认情况下,PropertySource 的工作方式如下:- 读取第一个属性文件- 读取第二个 (...) 属性文件 - 如果它包含一个已经给定的键,它将被覆盖.

By default the PropertySource is working like this: - Read first property file - Read second (...) property file - if it contains an already given key, it will be overridden.

在这里检查:http://javapapers.com/spring/spring-properties-with-propertysource-annotation/

如果您想将它与 XML 一起使用,请查看此答案:多位置的Spring属性占位符配置器中的属性解析顺序是什么?

If you want to use it with XML, check also this answer: What is property resolution order in Spring property placeholder configurer with multiple locations?

这篇关于Spring - 如何用外部文件覆盖内部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆