springboot启动会扫描一下位置的application.properties和applicatio.yml文件作为springboot的默认配置文件
1.文件加载优先级:(由高到低 四个部分的配置文件都会加载,但高优先级会覆盖低优先级配置)
-file:/config/
-file:/
classpath:/config/
calsspath:/
测试:
分别在根目录的config文件夹,根目录,和resources的config,resources目录下分别加入application.properties.
端口号分别是8084,8083,8082,8081
测试结果:优先级 根目录的config文件夹>根目录>resources的config>resources目录 (必须是config文件)
并且各个配置文件具有互补性
2.resources下的application.properties加入server.context-path = /hello
server.port = 8081
server.context-path = /hello
controller下的HelloServer类
package com.zzsd.priority_test.contorller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloServer {
@RequestMapping("/hello")
public String hello(){
return "配置文件具有互补性";
}
}
访问:locahost:8084/hello 和 locahost:8084/hello/hello
结果:


本文详细解析了SpringBoot中application.properties和application.yml配置文件的加载优先级,通过实验验证了不同目录下配置文件的覆盖关系,展示了配置文件的互补性,并提供了具体的测试案例。
855

被折叠的 条评论
为什么被折叠?



