springboot自动装配源码解析
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
private static Map
try {
Enumeration 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启动的时候就会一个个去加载开发人员配置的组件了。