Spring Boot 启动项目时,可以通过如下两个配置来指定配置文件:

  • spring.config.location
  • spring.config.additional-location

这两个配置都可以使用命令行参数或 JVM 属性来设置:

1
2
3
4
5
6
7
8
9
10
11
$ java -Dspring.config.location=E:\Documents\config\application-local.properties cn.z2huo.demo.spring.pkg.SpringPackageApp
$ java cn.z2huo.demo.spring.pkg.SpringPackageApp --spring.config.location=E:\Documents\config\application-local.properties

$ java -Dspring.config.additional-location=E:\Documents\config\application-local.properties cn.z2huo.demo.spring.pkg.SpringPackageApp
$ java cn.z2huo.demo.spring.pkg.SpringPackageApp --spring.config.additional-location=E:\Documents\config\application-local.properties

$ java -Dspring.config.location=E:\Documents\config\application-local.properties -jar myJar.jar
$ java -jar myJar.jar --spring.config.location=E:\Documents\config\application-local.properties

$ java -Dspring.config.additional-location=E:\Documents\config\application-local.properties -jar myJar.jar
$ java -jar myJar.jar --spring.config.additional-location=E:\Documents\config\application-local.properties

如果你不显式指定 spring.config.location,Spring Boot 会按照以下优先级顺序​(从高到低)自动在以下位置查找 application.propertiesapplication.yml 文件:

  • ​file:./config/,当前目录下的 config 文件夹
  • file:./​​,当前目录
  • classpath:/config/,类路径下的 config 文件夹
  • classpath:/ ​​,类路径根目录

高优先级的配置会覆盖低优先级的配置。

另外,这两个配置,可以指定文件,也可以指定目录。如果存在多个位置,需要使用逗号分隔多个配置。

通过 spring.config.location 指定位置的配置具有最高的优先级,会覆盖默认位置的配置和 spring.config.additional-location 指定位置的配置。也就是说,使用了 spring.config.location 指定配置文件之后,会忽略源项目中类路径下的配置文件,比如 resources/application.properties

如果指定的配置文件可能不存在,又希望应用不要因此启动失败,可以在路径前加上 optional:,示例如下:

1
java -Dspring.config.location=optional:E:\Documents\config\application-local.properties -jar myJar.jar

locationadditional-location 的区别

代码

location 会替换默认配置源,而 additional-location 是追加额外配置源。前者会导致原有配置失效,后者与原配置形成互补。

例如有下面的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootApplication  
public class SpringPackageApp {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringPackageApp.class, args);

ConfigurableEnvironment environment = context.getEnvironment();

System.out.println(environment.getProperty("spring.application.name"));
System.out.println(environment.getProperty("spring.config.location"));
System.out.println(environment.getProperty("spring.config.additional-location"));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component  
@Slf4j
public class ValueRunner implements CommandLineRunner {

@Value("${test.property.a}")
private String a;

@Value("${test.property.b}")
private String b;

@Value("${test.property.c:this is c default}")
private String c;

@Value("${test.property.d:this is d default}")
private String d;

@Override
public void run(String... args) throws Exception {
log.info("property a is {}, property b is {}, property c is {}, property d is {}", a, b, c, d);
}
}

项目的 resources 目录下有两个配置文件,分别是 application.propertiesapplication-local.properties,内容如下:

1
2
3
spring.application.name=spring-package-demo  

spring.profiles.include=local
1
2
3
4
5
test.property.a=a  

test.property.b=b

test.property.d=d

另外,在项目外部还有一个单独的配置文件 application-other.properties,内容如下:

1
2
3
4
5
test.property.a=aaa

test.property.b=bbb

test.property.c=ccc

现象

使用 spring.config.location

使用 --spring.config.location=E:\Documents\config\application-other.properties,输出结果如下:

1
2
3
4
property a is aaa, property b is bbb, property c is ccc, property d is this is d default
app name is null
spring.config.location is E:\Documents\config\application-other.properties
spring.config.additional-location is null

可以看到 resources/application.properties 文件中的 spring.application.name 配置输出为空。

resources/application-local.properties 文件中的 test.property.d 属性没有输出 d。并且,abc 三个属性都为 application-other.properties 中的配置,验证了:

通过 spring.config.location 指定位置的配置具有最高的优先级,会覆盖默认位置的配置和 spring.config.additional-location 指定位置的配置。

使用 spring.config.additional-location

使用 --spring.config.additional-location=E:\Documents\config\application-other.properties,输出结果如下:

1
2
3
4
property a is aaa, property b is bbb, property c is ccc, property d is d
app name is spring-package-demo
spring.config.location is null
spring.config.additional-location is E:\Documents\config\application-other.properties

可以看到,abc 三个属性输出的值都为 application-other.properties 文件中的值,application-other.properties 中不存在的 d 没有被覆盖掉,另外 spring.application.name 也正常输出了。

表示 spring.config.additional-location 是覆盖原有属性和补充属性,并不是对配置文件的完全替换。

相关链接

OB tags

#SpringBoot