springboot 工程如果项目有需求加载非本工程内的配置文件,我们应该怎么设置
SpringBoot 加载外部配置文件
默认加载
SpringBoot 默认加载配置文件是由SpringBoot默认声明的目录路径所加载,如果说是classpath下
配置方式
第一种
我们可以在SpringBoot启动类中设置环境参数,让SpringBoot加载所指定的目录
参数设置如下
SpringApplication application = new SpringApplication(SpringBootDemoApplication.class);
// 设置环境
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> myMap = new HashMap<>();
// 这里设置配置文件所在地址
myMap.put("spring.config.location", "D:\\");
propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
applica