Springboot配置文件加载位置

spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件

-file:./conifg/

-file:./

-classpath:/config/

-classpath:/

以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,

高优先级配置内容会覆盖低优先级配置内容。

Spring Boot会从这四个位置全部加载主配置文件;互补配置;

检查springboot版本为2.2.4,SpringBoot在2.0版本之后已经弃用server.context-path,而代替为server.servlet.context-path


我们也可以通过配置spring.config.location来改变默认配置

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定

配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成

互补配置;

外部配置加载顺序:

Spring Boot支持多种外部配置方式

这些方式优先级如下:

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config

SpringBoot 也可以从以下位置加载配置;优先级从高到低;搞优先级的配置

覆盖低优先级的配置,所有的配置会形成互补配置。

1,命令行参数

2,来自java:comp/env的JNDI属性

3.Java系统属性(System.getProperties())

4.操作系统环境变量

5.RandomValuePropertySource配置的random.*属性值

6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件

9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件

10.@Configuration注解类上的@PropertySource

11.通过SpringApplication.setDefaultProperties指定的默认属性

你可能感兴趣的:(Springboot配置文件加载位置)