在使用springboot时,很多配置我们都没有做,都是springboot在帮我们完成,这很大一部分归功于springboot自动装配,那springboot的自动装配的原理是怎么实现的呢?
自动装配原理
springboot 版本:2.4.3
SpringBootApplication
springboot启动类必须要加@SpringBootApplication注解,那这个注解是什么意思呢?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
抛开元数据注解来说,SpringBootApplication注解主要由@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan组成。这三个又有不同的作用如下:
- @SpringBootConfiguration:被@Configuration标记,表示这是个springboot配置,支持JavaConfig的方式来进行配置。
- @EnableAutoConfiguration:表示开启自动装配(重点介绍)
- @ComponentScan:扫描注解,扫描basePackages包下的bean并将他们注入到IOC容器中,比如:@Service、@Controller、@Component等注解。
EnableAutoConfiguration
真正开启自动配置的还是@EnableAutoConfiguration注解,来看下EnableAutoConfiguration注解源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
@EnableAutoConfiguration 又是由@AutoConfigurationPackage和@Import注解组成。
-
@AutoConfigurationPackage是一个复合注解的,它在内部使用@Import(AutoConfigurationPackages.Registrar.class)注解,Registrar是AutoConfigurationPackages的一个内部类,它的作用就是注册一个springboot启动类所在的包名,这个包名可以供列如JPA的使用。 -
AutoConfigurationImportSelector通过selectImports方法将配置类导入,从而完成bean的装配
AutoConfigurationImportSelector
AutoConfigurationImportSelector实现了DeferredImportSelector接口,DeferredImportSelector是ImportSelector的变种,它是一个延迟选择器。实现了DeferredImportSelector接口的子类如果重新了getImportGroup方法并返回DeferredImportSelector内部接口Group的子类,DeferredImportSelector接口的子类的子类将不会调用selectImports而是调用Group的selectImports方法。
接下来看看AutoConfigurationImportSelector重写了getImportGroup方法并返回一个内部类AutoConfigurationGroup,AutoConfigurationGroup#selectImports方法只是对配置数组进行排序筛选,真正处理自动配置的流程的是process方法。 process方法源码如下:
@Override
public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,
() -> String.format("Only %s implementations are supported, got %s",
AutoConfigurationImportSelector.class.getSimpleName(),
deferredImportSelector.getClass().getName()));
AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
.getAutoConfigurationEntry(annotationMetadata);
this.autoConfigurationEntries.add(autoConfigurationEntry);
for (String importClassName : autoConfigurationEntry.getConfigurations()) {
this.entries.putIfAbsent(importClassName, annotationMetadata);
}
}
process方法就是对AutoConfigurationGroup一些属性的填充,起作用的还是AutoConfigurationImportSelector.getAutoConfigurationEntry方法。 getAutoConfigurationEntry方法中经过各种判断过滤、去重等操作,最后返回AutoConfigurationEntry对象。源码如下
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
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 = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
getAutoConfigurationEntry 筛选的自动配置类:
getAutoConfigurationEntry方法中要重点分析的是getCandidateConfigurations方法。getCandidateConfigurations的作用是获取所有自动装配的配置类的全限定名。
来看下getCandidateConfigurations方法源码:
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
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;
}
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
这里面用到了SpringFactoriesLoader是spring提供的一种加载配置的方式,它会将类从配置文件中读取到,然后利用反射将bean加载到IOC容器中。
SpringFactoriesLoader.loadFactoryNames中会加载META-INF/spring.factories自动配置类。这些配置类在spring.factories文件中是以key=value的形式存储的,来看下部分自动配置类:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
来看下SpringFactoriesLoader.loadFactoryNames源码:
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
Map<String, List<String>> result = cache.get(classLoader);
if (result != null) {
return result;
}
result = new HashMap<>();
try {
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
String[] factoryImplementationNames =
StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
for (String factoryImplementationName : factoryImplementationNames) {
result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
.add(factoryImplementationName.trim());
}
}
}
result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
cache.put(classLoader, result);
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
return result;
}
loadFactoryNames方法只是传递一个org.springframework.boot.autoconfigure.EnableAutoConfiguration作为key,然后取到对应的自动配置类列表。 最终调用的是loadSpringFactories方法,loadSpringFactories会从jar包中找到spring.factories文件然后将其中的自动配置类存到一个map中,从下图可以看到map中存在很多bean,loadFactoryNames方法在加载自动配置类时只取了一个key。弱水三千,只取一瓢。
loadSpringFactories返回结果:
自动装配流程图大致如下:
总结
- EnableAutoConfiguration注解开启自动装配,其上的标记的@Import(AutoConfigurationImportSelector.class)注解中导入配置类
- AutoConfigurationImportSelector实现DeferredImportSelector接口,并重写了getImportGroup方法并返回AutoConfigurationImportSelector.AutoConfigurationGroup,AutoConfigurationImportSelector.AutoConfigurationGroup.process开始处理自动配置流程。
- AutoConfigurationImportSelector.getCandidateConfigurations获取所有配置类getAutoConfigurationEntry方法筛选,去重、移除不符合条件的自动配置类。
- SpringFactoriesLoader.loadSpringFactories从jar包中找到所有META-INF/spring.factories文件并读取自动配置类,存放到map中, loadFactoryNames方法通过全限定名org.springframework.boot.autoconfigure.EnableAutoConfiguration找到自动配置类。
- 最后经过层层筛选,去重、移除不符合条件的bean,由ConfigurationClassPostProcessor#processConfigBeanDefinitions注册所有的自动配置类。
能力一般,水平有限,如有错误,请多指出。 如果对你有用点个关注给个赞呗
|