application配置文件
介绍
- SpringBoot使用一个以application命名的配置文件作为默认的全局配置文件。支持properties后缀结尾的配置文件或者以yml/yaml后缀结尾的YAML的文件配置。
实现
application.properties
filepath=resources
@RestController
public class FirstController {
@Value("${filepath}")
private String filepath;
@GetMapping("info")
private Object info(){
return " filepath = " + filepath;
}
}
请求地址:http://localhost:8080/info
filepath = resources
注意:application.properties文件优先级别高于application.yaml文件
配置目录
介绍
SpringBoot配置文件可以放置在多种路径下,不同路径下的配置优先级有所不同。
优先级由低至高
- 类路径 (reousces/application.properties)
- 类路径config目录 (reousces/config/application.properties)
- 当前项目 (file:/application.properties)
- 当前项目config目录 (file:/config/application.properties)
实现
- src/main/resources/application.properties
filepath=resources
- src/main/resources/config/application.properties
filepath=resources-config
请求地址: http://localhost:8080/info
filepath = resources-config
源码中ConfigFileApplicationListener加载类
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/*/,file:./config/";