使用外部 application.properties
SpringApplication 默认会从以下位置加载 application.properties 文件
1.A /config subdirectory of the current directory : 当前目录的 config 目录下
2.The current directory : 当前目录下
3.A classpath /config package : classpath 根路径的 config 包下
4.The classpath root : classpath 根路径下
优先级 1 > 2 > 3 > 4 , 如四个位置都存在,则优先级高的将覆盖优先级低的,覆盖的是属性并可叠加更多属性
Spring 还提供更多其他方式指定配置文件,请参考官方文档中 Externalized Configuration 章节
使用外部配置文件覆盖内部自定义配置文件的属性
通过 @PropertySource 注解,并定义多个路径,后面的属性将覆盖前面的,如:
@Configuration
@PropertySource(value = { "classpath:config.properties", "file:./config/config.properties" }, ignoreResourceNotFound = true)
ignoreResourceNotFound = true,忽略找不到时的错误提示,否则开发环境编译不过,因为找不到这个路径下的文件
在需要覆盖属性时,只需在 jar 同级目录中放入 config 文件夹并在其中放入 config.properties 文件 (同 application.properties 1 中的方式)