springboot 获取resource文件路径_SpringBoot | 是如何实现自动配置的?

f8e7d6aafc0c4da252a043b32c8e0aa3.png

配置文件能写什么?

相信接触过 SpringBoot 的朋友都知道 SpringBoot 有各种 starter 依赖,想要什么直接勾选加进来就可以了。想要自定义的时候就直接在配置文件写自己的配置就好。但你们有没有困惑,为什么 SpringBoot 如此智能,到底配置文件里面能写什么呢?

带着这个疑问,我翻了下 SpringBoot 官网看到这么一些配置样例:

56cc55a40172ef562ed8d09dccebabd3.png

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

@SpringBootApplication 源码

SpringBoot 启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration,再进入 @EnableAutoConfiguration 源码:

3302a6114eae1e3700c2d59c692b0b64.png

@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;

}

代码注释很清楚:

  1. 首先注意到 selectImports 方法,其实从方法名就能看出,这个方法用于给容器中导入组件,然后跳到 getAutoConfigurationEntry 方法就是用于获取自动配置项的。
  2. 再来进入 getCandidateConfigurations 方法就是 获取一个自动配置 List ,这个 List 就包含了所有的自动配置的类名 。
  3. 再进入 SpringFactoriesLoader 类的 loadFactoryNames 方法,跳转到 loadSpringFactories 方法发现 ClassLoader 类加载器指定了一个 FACTORIES_RESOURCE_LOCATION 常量。
  4. 然后利用 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 + "]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值