标签:一个 file appdata ade ash 项目 etl img opera
spring boot 配置文件(application.yaml / xml)优先级:官方文档
file:./config/
- 优先级最高(项目根路径下的config
)
file:./
- 优先级第二 -(项目根路径下)
classpath:/config/
- 优先级第三(项目resources/config
下)
classpath:/
- 优先级第四(项目resources
根目录)
使用spring.profiles.active = ??? 切换配置环境,如下 |
spring
profiles
spring.profiles.active=配置文件名
但是配置文件太多文件目录可能有点乱,那么yaml的优势就来了,如果你有多个配置,可以放在一个yaml中
server
如果我们要选择要用的配置,可以使用spring.profiles.active=配置名在一个yaml文件中切换(没有profiles属性的是默认选择)。
spring boot的@SpringBootApplication下的源码如下
@EnableAutoConfiguration 和自动配置相关,打开源码
?
在@EnableAutoConfiguration下存在@Import({AutoConfigurationImportSelector.class}) ,这个就是选择导入,从这里进入找到
getAutoConfigurationEntry(省略了很多),这个实体从
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);这个里面获得实体,然后或偶去配置,获取配置从
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}这里获取配置this.getBeanClassLoader() 标注了注解类,所以能够找到,在上面的private ClassLoader beanClassLoader;这里,
然后从这里在这里返回已经标志了的类,如下
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
从this.getSpringFactoriesLoaderFactoryClass(), 这里返回
protected ClassLoader getBeanClassLoader() {
return this.beanClassLoader;
}spring boot读取的资源自动装配,最终指向配置文件FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories",源码如下
public final class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap();
?
private SpringFactoriesLoader() {
}
META-INF/spring.factories这个文件特别牛批!!!!!!我们的配置和它密切相关,无论哪一个,都带有注释@Configration 被配置!!!!!spring帮我们配置了很多东西,我们只需要直接用,spring boot牛皮!!!!!!!!!!!!!!!!
spring的底层注解@CondionalOnXXXX,根据不同的条件,判断当前配置或者类是否生效!!!!!!!!!
标签:一个 file appdata ade ash 项目 etl img opera
原文地址:https://www.cnblogs.com/tianjin/p/13844085.html