配置文件加载位置
加载优先级
假定项目路径为project/,
SpringBoot启动会扫描以下位置的application.properties或者application.yml文件作为SpringBoot的默认配置文件
- /project/config/
- /project
- classpath:/config/
- classpath:/
优先级从高到低,所有配置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。
另外我们可以通过配置spring.config.location
来改变默认配置,用于项目打包以后,使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用,形成互补配置。
spring.config.location测试
在classpath:/config下新建application.yml:
server:
port: 3333
在K盘下新建application.yml:
server:
port: 5555
admin:
name: Khighness
编写controller:
@RestController
public class HelloController {
@Value("${admin.name}")
public String adminName;
@GetMapping("/name")
public String hello() {
return adminName;
}
}
在项目根路径下运行如下命令:
$ mvn apckage
$ cd target
$ java -jar springboot-basic-1.0-SNAPSHOT.jar --spring.config.location=K:/application.yml
运行结果:
██╗ ██╗██╗ ██╗██╗ ██████╗ ██╗ ██╗███╗ ██╗███████╗███████╗███████╗
██║ ██╔╝██║ ██║██║██╔════╝ ██║ ██║████╗ ██║██╔════╝██╔════╝██╔════╝
█████╔╝ ███████║██║██║ ███╗███████║██╔██╗ ██║█████╗ ███████╗███████╗
██╔═██╗ ██╔══██║██║██║ ██║██╔══██║██║╚██╗██║██╔══╝ ╚════██║╚════██║
██║ ██╗██║ ██║██║╚██████╔╝██║ ██║██║ ╚████║███████╗███████║███████║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝╚══════╝
2021-04-14 22:41:21.494 INFO 1208 --- [ main] top.parak.KhighnessApplication : Starting KhighnessApplication v1.0-SNAPSHOT on KHighness-Dell with PID 1208 (D:\Java\springboot\springboot-basic\target\springboot-basic-1.0-SNAPSHOT.jar started by 18236 in D:\Java\springboot\springboot-basic\target)
2021-04-14 22:41:21.497 INFO 1208 --- [ main] top.parak.KhighnessApplication
: No active profile set, falling back to default profiles: default
2021-04-14 22:41:22.344 INFO 1208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 5555 (http)
2021-04-14 22:41:22.354 INFO 1208 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-14 22:41:22.354 INFO 1208 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2021-04-14 22:41:22.355 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
2021-04-14 22:41:22.356 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2021-04-14 22:41:22.356 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2021-04-14 22:41:22.358 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1c 28 May 2019]
2021-04-14 22:41:22.409 INFO 1208 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-04-14 22:41:22.409 INFO 1208 --- [ main] o.s.web.context.ContextLoader
2021-04-14 22:41:22.560 INFO 1208 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-14 22:41:22.634 INFO 1208 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [public/index.html]
2021-04-14 22:41:22.716 INFO 1208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 5555 (http) with context path ''
2021-04-14 22:41:22.718 INFO 1208 --- [ main] top.parak.KhighnessApplication : Started KhighnessApplication in 1.526 seconds (JVM running for 1.819)
接口测试:
$ curl http://localhost:5555/name
StatusCode : 200
StatusDescription :
Content : Khighness
RawContent : HTTP/1.1 200
Keep-Alive: timeout=60
Connection: keep-alive
Content-Length: 9
Content-Type: text/plain;charset=UTF-8
Date: Wed, 14 Apr 2021 14:46:15 GMT
Khighness
Forms : {}
Headers : {[Keep-Alive, timeout=60], [Connection, keep-alive], [Content-Length, 9], [Content-Type, text/plain;charset=UTF-8]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 9
外部配置加载顺序
官方文档
📖 boot-features-external-config
官方文档列举了17种:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). [@TestPropertySource](https://docs.spring.io/spring/docs/4.3.13.RELEASE/javadoc-api/org/springframework/test/context/TestPropertySource.html)
annotations on your tests.[@SpringBootTest#properties](https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/api/org/springframework/boot/test/context/SpringBootTest.html)
annotation attribute on your tests.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property) ServletConfig
init parameters.ServletContext
init parameters.- JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that only has properties inrandom.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants) - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants) - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). [@PropertySource](https://docs.spring.io/spring/docs/4.3.13.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html)
annotations on your@Configuration
classes.- Default properties (specified using
SpringApplication.setDefaultProperties
).
比较重要
SpringBoot从以下位置加载配置,按照优先级从上至下;
高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置。
(1)命令行参数(java -jar
(2)来自java:comp/env是JNDI属性
(3)Java系统属性(System.getProperties)
(4)操作系统环境变量
(5)RandomValuePropertySource配置random.*属性值
(6)jar包外部的application-{profilr}.properties或application.yml(带spring.profile)配置文件
(7)jar包内部的application-{profilr}.properties或application.yml(带spring.profile)配置文件
(8)jar包外部的applicatio.properties或application.yml(不带spring.profile)配置文件
(9)jar包内部的applicatio.properties或application.yml(不带spring.profile)配置文件
(10)@Configuration注解类上的@PropertySource
总结
最先加载命令行和环境属性;
优先加载带profile,再加载不带profile,由jar外至jar包内;
最后加载注解。