写点什么

springboot 自动装配源码解析

用户头像
偏执
关注
发布于: 1 小时前

​springboot 作为一个优秀的脚手架的框架,封装集成了很多组件功能,比如以前要初始化一个 springmvc+spring 的框架,需要配置很多 xml 文件才能完成,springboot 就将类似很多配置集成了,开发人员只需要简单的注解或者配置文件就可以完成框架搭建,对初级开发使用人员很友好。springboot 的思想是约定>配置,只要按照 springboot 的配置,开发人员便很容易上手。


但是,开发人员需要配置的东西越少,则表明封装的程度越高,黑盒程度越高,所有我们有必要对 springboot 的源码就行阅读,了解其原理,这样遇到问题的时候才能更快速解决。


刚好,笔者也是刚接触 springboot、springcloud 的不久,以前用的是 dubbo 框架,都是 alibaba 开发的,这一次就记录自己学习的过程。在看了 sping 和 dubbo 的源码后,发现再看这些中间件的源码不是很难,spring 是基础,相当于武侠里面的九阳神功,只要内功好,其他功夫学起来就相对简单了,很多中间件框架会利用 spring 的扩展点和 spring 集成。


springboot 和 springcloud 的关系 springboot 是 springcloud 的基础,springcloud 作为 alibaba 开发的微服务全家桶,集成了很多组件,如 nacos 注册中心、ribbon、feign、sentinel 等,后面也会一一讲解。


什么叫自动装配假设开发人员要使用 springcloud 的 nacos 组件,只要在 pom 配置文件中输入 nacos 的 maven 仓库地址即可,springboot 启动容器的时候就会自动去加载 nacos 组件。 org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 那么他是怎么实现的呢?


流程图地址:www.processon.com/view/link/6…


入口在每个启动程序 XXXApplication 中,都需要加 @SpringBootApplication 注解,标明是一个 springboot 应用,点进这个注解,进入 @EnableAutoConfiguration 注解


@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {


String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */Class<?>[] exclude() default {};
/** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */String[] excludeName() default {};
复制代码


}复制代码可以看到,这里 @Import 导入了一个 AutoConfigurationImportSelector 类,这个类很关键,是 springboot 和 spring 集成的关键点,AutoConfigurationImportSelector 实现了 ImportSelector,这个类型的 selector 是一个延迟加载的类,在 spring 容器中优先级最低,会等 spring 其他 bean 定义加载完再加载,最终会调用该类的 selectImports 方法。


从 spring.factories 读取配置类在 AutoConfigurationImportSelector.selectImports 方法中,有一个 getCandidateConfigurations 方法,进入这个方法 SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader())


public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AnnotationAttributes attributes = getAttributes(annotationMetadata);List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);configurations = removeDuplicates(configurations);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return StringUtils.toStringArray(configurations);}复制代码 SpringFactoriesLoader.loadFactoryNames 这里可以看到,会去加载 META-INF/spring.factories 这个配置文件,并且将配置文件中的配置类解析出来,spring 在调用完这个方法后,会将解析出来的类加载成配置文件,具体参考 spring 代码 org.springframework.context.annotation.ConfigurationClassParser#parse ConfigurationClassParser#processImports


private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = cache.get(classLoader);if (result != null) {return result;}


  try {    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 = PropertiesLoaderUtils.loadProperties(resource);      for (Map.Entry<?, ?> entry : properties.entrySet()) {        String factoryClassName = ((String) entry.getKey()).trim();        for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {          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 + "]", ex);  }}
复制代码


复制代码


举例看一个配置类,随便打开了一个 start-ribbon 组件,可以看到这里配置了一个 RibbonAutoConfiguration 类,而这个类上面加了 spring 的注解 @Configuration,所以也会在 spring 启动的时候调用,看到这里,spring 的自动装配基本就讲完了


总结一路看下来,自动装配的原理还是相对简单的,总得来说,就是通过导入一个 spring 的扩展点类 AutoConfigurationImportSelector,去读取 META-INF/spring.factories 文件,将文件中的配置利用 spring 的扩展点方法 processImport 加载成配置类,这样 springboot 启动的时候就会一个个去加载开发人员配置的组件了。


用户头像

偏执

关注

还未添加个人签名 2021.07.23 加入

还未添加个人简介

评论

发布
暂无评论
springboot自动装配源码解析