注意:此文指针对所有配置文件都是yml的引用情况
刚开始建的项目查看依赖是否有引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在实际开发中项目指识别application和bootstrap开头的文件,对于自定义名称的文件不能识别,那自定义配置文件如何识别呢?
自定义识别文件有两种方式:
方式一:在启动类中加载配置
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("hip-server-config.yml"));
configurer.setProperties(yaml.getObject());
return configurer;
}
方式二:采用spring-cloud的配置中心
集成spring-cloud config后,默认根据应用名称加载(application:name),如果需要配置多个配置文件,需要修改配置
图中右侧是resource目录下的bootstrap-dev.yml文件部分内容