![f8e7d6aafc0c4da252a043b32c8e0aa3.png](https://i-blog.csdnimg.cn/blog_migrate/5f3f9dd0f7e63bf6173e6df6a691f320.jpeg)
配置文件能写什么?
相信接触过 SpringBoot 的朋友都知道 SpringBoot 有各种 starter 依赖,想要什么直接勾选加进来就可以了。想要自定义的时候就直接在配置文件写自己的配置就好。但你们有没有困惑,为什么 SpringBoot 如此智能,到底配置文件里面能写什么呢?
带着这个疑问,我翻了下 SpringBoot 官网看到这么一些配置样例:
![56cc55a40172ef562ed8d09dccebabd3.png](https://i-blog.csdnimg.cn/blog_migrate/5d8c7a279f9bda4d22f19e4d2572b44a.jpeg)
SpringBoot 配置样例
发现 SpringBoot 可配置的东西非常多,上图只是节选。有兴趣的查看这个网址:
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml
自动配置原理
这里我拿之前创建过的 SpringBoot 来举例讲解 SpringBoot 的自动配置原理,首先看这么一段代码:
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
毫无疑问这里只有 @SpringBootApplication 值得研究,进入 @SpringBootApplication 的源码:
![1b45c1b54f084a9011ddccaf1990cc09.png](https://i-blog.csdnimg.cn/blog_migrate/3f644e4275d668bb513d9df8ba34805c.jpeg)
@SpringBootApplication 源码
SpringBoot 启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration,再进入 @EnableAutoConfiguration 源码:
![3302a6114eae1e3700c2d59c692b0b64.png](https://i-blog.csdnimg.cn/blog_migrate/a90c121af279c2f512a86df76b5fc3c8.jpeg)
@EnableAutoConfiguration 源码
发现最重要的就是 @Import(AutoConfigurationImportSelector.class) 这个注解,其中的 AutoConfigurationImportSelector 类的作用就是往 Spring 容器中导入组件,我们再进入这个类的源码,发现有这几个方法:
/**
* 方法用于给容器中导入组件
**/
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
autoConfigurationMetadata, annotationMetadata); // 获取自动配置项
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
// 获取自动配置项
protected AutoConfigurationEntry getAutoConfigurationEntry(
AutoConfigurationMetadata autoConfigurationMetadata,
AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List < String > configurations = getCandidateConfigurations(annotationMetadata,
attributes); // 获取一个自动配置 List ,这个 List 就包含了所有自动配置的类名
configurations = removeDuplicates(configurations);
Set < String > exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
// 获取一个自动配置 List ,这个 List 就包含了所有的自动配置的类名
protected List < String > getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
// 通过 getSpringFactoriesLoaderFactoryClass 获取默认的 EnableAutoConfiguration.class 类名,传入 loadFactoryNames 方法
List < String > configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), 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;
}
// 默认的 EnableAutoConfiguration.class 类名
protected Class> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
代码注释很清楚:
- 首先注意到 selectImports 方法,其实从方法名就能看出,这个方法用于给容器中导入组件,然后跳到 getAutoConfigurationEntry 方法就是用于获取自动配置项的。
- 再来进入 getCandidateConfigurations 方法就是 获取一个自动配置 List ,这个 List 就包含了所有的自动配置的类名 。
- 再进入 SpringFactoriesLoader 类的 loadFactoryNames 方法,跳转到 loadSpringFactories 方法发现 ClassLoader 类加载器指定了一个 FACTORIES_RESOURCE_LOCATION 常量。
- 然后利用 PropertiesLoaderUtils 把 ClassLoader 扫描到的这些文件的内容包装成 properties 对象,从 properties 中获取到 EnableAutoConfiguration.class 类(类名)对应的值,然后把他们添加在容器中。
public static List < String > loadFactoryNames(Class < ? > factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
private static Map < String, List < String >> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap < String, String > result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
// 扫描所有 jar 包类路径下 META-INF/spring.factories
Enumeration < URL > urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap < > ();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
// 把扫描到的这些文件的内容包装成 properties 对象
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry < ? , ? > entry : properties.entrySet()) {
String factoryClassName = ((String) entry.getKey()).trim();
for (String factoryName: StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
// 从 properties 中获取到 EnableAutoConfiguration.class 类(类名)对应的值,然后把他们添加在容器中
result.add(factoryClassName, factoryName.trim());
}
}
}
cache.put(classLoader, result);
return result;
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]