1.配置文件加载

1.1 加载配置路径

Springboot 启动时会扫描指定位置的下的配置文件[application.properties或者application.yml]作为Springboot的默认配置文件,如果多个位置都有配置文件时,则高优先级的配置会覆盖低优先级的配置。

Springboot 启动时会扫描指定位置如下:

-file:./config/
-file:./
-classpath:/config/
-classpath:/

优先级由高到底,高优先级的配置会覆盖低优先级的配置;

1.2 测试路径优先级

1. 创建多个配置文件

新建配置文件A: config-file/config/application.yml

server:
port: 8001

#标记配置文件位置
config:
path: config-file/config/application.yml
desc: 描述:AAA...

新建配置文件B: config-file/application.yml

server:
port: 8002

#标记配置文件位置
config:
path: config-file/application.yml
desc: 描述:BBB...

新建配置文件C: config-file/src/main/resources/config/application.yml

server:
port: 8003

#标记配置文件位置
config:
path: config-file/src/main/resources/config/application.yml
desc: 描述:CCC...

新建配置文件D: config-file/src/main/resources/application.yml

server:
port: 8004

#标记配置文件位置
config:
path: config-file/src/main/resources/application.yml
desc: 描述:DDD...

2. 创建测试控制器

新建文件: configfile/controller/TestController.java

package com.hui.configfile.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
@Value("${config.path}")
private String configPath;
@Value("${config.desc}")
private String configDesc;

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "配置文件位置: " + configPath + "\r\n 配置描述: " + configDesc;
}
}
/*
启动时,加载的是配置文件A,依次删除desc,则输出以下内容:

配置文件位置: config-file/config/application.yml 配置描述: 描述:AAA...
配置文件位置: config-file/config/application.yml 配置描述: 描述:BBB...
配置文件位置: config-file/config/application.yml 配置描述: 描述:CCC...
配置文件位置: config-file/config/application.yml 配置描述: 描述:DDD...
*/

Springboot会从这四个位置全部加载配置文件,从而达到互补配置;

2. 多配置文件

当我们需要以多环境区分配置文件的时候,配置文件名可以是 application-{env}.properties/yml的格式来命名,然后通过默认配置文件application.yml 中的spring.active属性来指定具体环境, 如常见的: application-test.yml、application-prep.yml、application-prod.yml

2.1 创建多配置文件

新建测试环境配置文件: src/main/resources/config/application-test.yml

server:
port: 8004
env: 这是测试环境

新建测试环境配置文件: src/main/resources/config/application-prep.yml

server:
port: 8005
env: 这是预发环境

2.2 指名具体环境使用

新建默认环境配置文件: src/main/resources/config/application.yml并指定使用哪个环境的配置:

spring:
profiles:
active: test

新建测试控制器: src/main/java/com/hui/configfile/controller/TestController.java

package com.hui.configfile.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
@Value("${env}")
private String env;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "启动配置所属环境: " + env;
}
}

3. 命令行配置

3.1 打包SpringBoot

打包命令 : mvn clean package

...
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ config-file ---
[INFO] Building jar: /Users/hui/Project/Java/config-file/target/config-file-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ config-file ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.035 s
[INFO] Finished at: 2020-10-13T19:47:05+08:00
[INFO] ------------------------------------------------------------------------

3.2 运行Jar包

➜  config-file git:(priority) ✗ java -jar target/config-file-0.0.1-SNAPSHOT.jar --spring.profiles.active=prep

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)

2020-10-13 19:52:03.911 WARN 7379 --- [ main] o.s.boot.StartupInfoLogger : InetAddress.getLocalHost().getHostName() took 5003 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2020-10-13 19:52:08.921 INFO 7379 --- [ main] c.hui.configfile.ConfigFileApplication : Starting ConfigFileApplication v0.0.1-SNAPSHOT on huideMacBook-Pro.local with PID 7379 (/Users/hui/Project/Java/config-file/target/config-file-0.0.1-SNAPSHOT.jar started by hui in /Users/hui/Project/Java/config-file)
2020-10-13 19:52:08.922 INFO 7379 --- [ main] c.hui.configfile.ConfigFileApplication : The following profiles are active: prep
2020-10-13 19:52:10.124 INFO 7379 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8005 (http)
2020-10-13 19:52:10.148 INFO 7379 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-10-13 19:52:10.150 INFO 7379 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-13 19:52:10.261 INFO 7379 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-10-13 19:52:10.261 INFO 7379 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1264 ms
2020-10-13 19:52:10.509 INFO 7379 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-13 19:52:10.741 INFO 7379 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8005 (http) with context path ''
2020-10-13 19:52:10.756 INFO 7379 --- [ main] c.hui.configfile.ConfigFileApplication : Started ConfigFileApplication in 17.464 seconds (JVM running for 17.966)

多个配置用;隔开。

由上面启动信息可以看到,监听的端口号是: 8005

# 访问
➜ config-file git:(priority) ✗ curl 127.0.0.1:8005/hello
启动配置所属环境: 这是预发环境