Spring Boot(六)----application.properties文件加载顺序
一.配置文件加载顺序
SpringBoot会扫描如下几个位置的application.properties文件以及application.yml文件作为Springboot的默认配置文件,且位置的不同会造成文件的加载优先级不同。
文件位置如下:
-file:./config/
-file:./
-classpath:/config/
-classpath:/

以上是按照优先级从高到低的顺序进行加载,所有的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。
我们也可以通过配置spring.config.location来改变默认配置。
spring.config.location
可以通过在配置文件中写入端口号进行访问的形式,判断配置文件加载顺序的不同。
两个有限原则:
1.优先加载文件目录下的配置文件
2.优先加载config目录下的配置文件
举个例子

DemoApplication.java
package com.zk.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@ImportResource(locations = "classpath:beans.xml")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(com.zk.demo.DemoApplication.class, args);
}
}
/resources/config/application.properties

# 应用名称 spring.application.name=demo # 应用服务 WEB 访问端口 server.port=8082
/resources/application.properties

# 应用名称 spring.application.name=demo # 应用服务 WEB 访问端口 server.port=8081
运行端口号为:

说明/resources/config目录下的application.properties优先级比/resources目录下的application.properties要高。
SpringBoot也会从这四个位置加载全部的配置文件,形成配置文件间的互补。

增加一个HelloController.java
package com.zk.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
增加hello.jsp
<%--
Created by IntelliJ IDEA.
User: vitue
Date: 2022/8/10
Time: 22:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
hello
</body>
</html>
在上面写的/resources/application.properties文件中加入如下代码
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8081
server.context-path=/boot2

测试,访问localhost:8082/hello,可以看到上面仍然访问的是8082端口,形成互补配置

访问localhost:8082/boot2/hello

二.加载外部配置文件
如果不想使用application.yml或者application.properties文件作为配置文件,可以使用如下代码进行加载配置文件
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
或者
java -jar -Dspring.config.location=D:\config\config.properties springbootrestdemo-0.0.1-SNAPSHOT.jar
也可以在代码中直接配置@PropertySource
@SpringBootApplication
@PropertySource(value={"file:config.properties"})
public class SpringbootrestdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootrestdemoApplication.class, args);
}
}
SpringBoot也可以从以下位置加载配置;优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置
1、命令行参数
2、来自java:comp/env的NDI属性
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)配置文件

浙公网安备 33010602011771号