SpringBoot 2.x 系列:自动装配
首页 专栏 java 文章详情
1
头图

SpringBoot 2.x 系列:自动装配

黑色的灯塔 发布于 2 月 25 日

概览

上一节介绍了SpringBoot中的配置体系,接下来将会介绍SpringBoot的中最基础、最核心的自动装配(AutoConfiguration)机制。正是有了自动装配机制,我们可以很快的搭建一个Web项目,实现零配置,

看下SpringBoot是如何帮忙我们实现自动装配的,我们首先从@SpringBootApplication注解说起:

自动装配过程

@SpringBootApplication 启动类注解

每个SpringBoot项目都会有一个启动类,该类位于代码的根目录下,一般用XXXApplication命名,@SpringBootApplication注解会添加到该类上。@SpringBootApplication 注解位于 spring-boot-autoconfigure 工程的 org.springframework.boot.autoconfigure 包中,定义如下:

@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 {
 @AliasFor(annotation = EnableAutoConfiguration.class)
 Class<?>[] exclude() default {};
 
 @AliasFor(annotation = EnableAutoConfiguration.class)
 String[] excludeName() default {};
 
 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
 String[] scanBasePackages() default {};
 
 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
 Class<?>[] scanBasePackageClasses() default {};
}

该注解相比较显得有点复杂,从定义上看,@SpringBootApplication注解是一个组合注解,它是由三个注解组合而成,分别是:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

我们可以通过exclude 和 excludeName 属性来配置不需要实现自动装配的类或类名,也可以通过 scanBasePackages 和 scanBasePackageClasses 属性来配置需要进行扫描的包路径和类路径。接下来我们分别对这三个注解一一介绍:

@SpringBootConfiguration

@SpringBootConfiguration注解比较简单,定义如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
 @AliasFor(
 annotation = Configuration.class
 )
 boolean proxyBeanMethods() default true;
}

从定义来看,@SpringBootConfiguration等同于@Configuration注解,@Configuration比较常见,用来标识该类是JavaConfig配置类

@ComponentScan

@ComponentScan 注解不是 Spring Boot 引入的新注解,而是属于 Spring 容器管理的内容。@ComponentScan 注解就是扫描基于 @Component 等注解所标注的类所在包下的所有需要注入的类,并把相关 Bean 定义批量加载到容器中。

@EnableAutoConfiguration 自动配置注解

@EnableAutoConfiguration 这个注解一看名字就很牛,它是SpringBoot实现自动装配的核心注解,先看定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
​
 String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
​
 Class<?>[] exclude() default {};
​
 String[] excludeName() default {};
​
}

又是一个组合注解,是由@AutoConfigurationPackage和@Import(AutoConfigurationPackages.Registrar.class)组合成,下面分别来介绍:

@AutoConfigurationPackage 注解

@AutoConfigurationPackage注解定义如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
​
}

该注解的作用是将添加该注解的类所在的包作为自动装配的包路径进行管理。在@AutoConfigurationPackage注解的定义中,我们又发现了一个@Import注解,@Import 注解是由 Spring 提供的,作用是将某个类实例化并加入到 Spring IoC 容器中。所以我们要想知道 @Import(AutoConfigurationPackages.Registrar.class) 究竟做了什么就需要了解Registrar这个类里包含了哪些方法。

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
​
 @Override
 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
 register(registry, new PackageImport(metadata).getPackageName());
 }
​
 @Override
 public Set<Object> determineImports(AnnotationMetadata metadata) {
 return Collections.singleton(new PackageImport(metadata));
 }
​
}

Registrar类里一共有两个方法,分别是determineImports和registerBeanDefinitions,

new PackageImport(metadata).getPackageName()返回的就是@SpringBootApplication注解所在的类的包名

@Import(AutoConfigurationImportSelecor.class) 自动导入配置文件选择器

接下来我们回到@EnableAutoConfiguration注解中的 @Import(AutoConfigurationImportSelecor.class) 部分来,

这个类下有个重要的方法selectImports方法,Spring会把该方法返回的所有的类加载到IOC的容器中。所以这个类的主要作用是选择Spring加载哪些类到IOC容器中。

@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);
 
 //获取 configurations集合
 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 new AutoConfigurationEntry(configurations, exclusions);
 }

这段代码的核心是通过 getCandidateConfigurations 方法获取 configurations 集合并进行过滤。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;
}

这里我们重点关注下SpringFactoriesLoader.loadFactoryNames()方法,该方法会从META-INF/spring.factories文件中去查找自动配置的类,在这里,不得不提到JDK的SPI机制,因为无论从 SpringFactoriesLoader 这个类的命名上,还是 META-INF/spring.factories 这个文件目录,两者之间都存在很大的相通性。

Java SPI 机制和 SpringFactoriesLoader

要想理解 SpringFactoriesLoader 类,我们首先需要了解 JDK 中 SPI(Service Provider Interface,服务提供者接口)机制。简单的说SPI机制就是为接口寻找服务实现的机制,有点类似Spring的IOC的思想,不过将自动装配的控制权移到程序外部,可以避免程序中使用硬编码,在模块化设计中这种机制尤为重要。使用Java SPI机制需要以下几步:

当服务提供者提供了接口的一种具体实现后,在jar包的META-INF/services目录下创建一个以“接口全路径名”为命名的文件,内容为实现类的全路径名 当外部程序装配这个 jar 包时,就能通过该 jar 包 META-INF/services/ 目录中的配置文件找到具体的实现类名,并装载实例化,从而完成模块的注入

SpringFactoriesLoader 类似这种 SPI 机制,只不过以服务接口命名的文件是放在 META-INF/spring.factories 文件夹下,定义了一个key为EnableAutoConfiguration,SpringFactoriesLoader 会查找所有 META-INF/spring.factories 文件夹中的配置文件,并把 Key 为 EnableAutoConfiguration 所对应的配置项通过反射实例化为配置类并加载到容器中。

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 factoryTypeName = ((String) entry.getKey()).trim();
 for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
 result.add(factoryTypeName, factoryImplementationName.trim());
 }
 }
 }
 cache.put(classLoader, result);
 return result;
 }
 catch (IOException ex) {
 throw new IllegalArgumentException("Unable to load factories from location [" +
 FACTORIES_RESOURCE_LOCATION + "]", ex);
 }
}

以上就是SpringBoot中基于 @SpringBootApplication注解实现自动装配的基本过程和原理,不过SpringBoot默认情况下给我们提供了100多个AutoConfiguration的类

# Auto Configure
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.CloudServiceConnectorsAutoConfiguration,
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,

显然我们不可能把所有的类全部引入,所以在自动装配的时候,需要会根据类路径去寻找是否有对应的配置类,如果有的话还需要按照条件进行判断,来决定是否要装配,这里我要引出SpringBoot中经常要用到的另外一批注解@ConditionalOn系列注解。

@ConditionalOn 系列条件注解

当我们构建一个 Spring 应用的时候,有时我们想在满足指定条件的时候才将某个 bean 加载到应用上下文中, 在Spring 4.0 时代,我们可以通过 @Conditional 注解来实现这类操作,SpringBoot在 @Conditional注解的基础上进行了细化,定义了一系列的@ConditionalOn条件注解:

@ConditionalOnProperty:只有当所提供的属性属于 true 时才会实例化 Bean @ConditionalOnBean:只有在当前上下文中存在某个对象时才会实例化 Bean @ConditionalOnClass:只有当某个 Class 位于类路径上时才会实例化 Bean @ConditionalOnExpression:只有当表达式为 true 的时候才会实例化 Bean @ConditionalOnMissingBean:只有在当前上下文中不存在某个对象时才会实例化 Bean @ConditionalOnMissingClass:只有当某个 Class 在类路径上不存在的时候才会实例化 Bean @ConditionalOnNotWebApplication:只有当不是 Web 应用时才会实例化 Bean

由于@ConditionalOn 系列条件注解非常多,我们无意对所有这些组件进行展开。事实上这些注解的实现原理也大致相同,我们只需要深入了解其中一个就能做到触类旁通。这里我们挑选 @ConditionalOnClass 注解进行展开,该注解定义如下:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
  Class<?>[] value() default {};
  String[] name() default {};
}

可以看到, @ConditionalOnClass 注解本身带有两个属性,一个 Class 类型的 value,一个 String 类型的 name,所以我们可以采用这两种方式中的任意一种来使用该注解。同时 ConditionalOnClass 注解本身还带了一个 @Conditional(OnClassCondition.class) 注解。所以, ConditionalOnClass 注解的判断条件其实就包含在 OnClassCondition 这个类中。

OnClassCondition 是 SpringBootCondition 的子类,而 SpringBootCondition 又实现了Condition 接口。Condition 接口只有一个 matches 方法,如下所示:

@Override
public final boolean matches(ConditionContext context,
            AnnotatedTypeMetadata metadata) {
        String classOrMethodName = getClassOrMethodName(metadata);
        try {
            ConditionOutcome outcome = getMatchOutcome(context, metadata);
            logOutcome(classOrMethodName, outcome);
            recordEvaluation(context, classOrMethodName, outcome);
            return outcome.isMatch();
        }
        //省略其他方法
}

这里的 getClassOrMethodName 方法获取被添加了@ConditionalOnClass 注解的类或者方法的名称,而 getMatchOutcome 方法用于获取匹配的输出。我们看到 getMatchOutcome 方法实际上是一个抽象方法,需要交由 SpringBootCondition 的各个子类完成实现,这里的子类就是 OnClassCondition 类。在理解 OnClassCondition 时,我们需要明白在 Spring Boot 中,@ConditionalOnClass 或者 @ConditionalOnMissingClass 注解对应的条件类都是 OnClassCondition,所以在 OnClassCondition 的 getMatchOutcome 中会同时处理两种情况。这里我们挑选处理 @ConditionalOnClass 注解的代码,核心逻辑如下所示:

ClassLoader classLoader = context.getClassLoader();
ConditionMessage matchMessage = ConditionMessage.empty();
List<String> onClasses = getCandidates(metadata, ConditionalOnClass.class);
if (onClasses != null) {
            List<String> missing = getMatches(onClasses, MatchType.MISSING, classLoader);
            if (!missing.isEmpty()) {
                return ConditionOutcome
                        .noMatch(ConditionMessage.forCondition(ConditionalOnClass.class)
                                .didNotFind("required class", "required classes")
                                .items(Style.QUOTE, missing));
            }
            matchMessage = matchMessage.andCondition(ConditionalOnClass.class)
                    .found("required class", "required classes").items(Style.QUOTE, getMatches(onClasses, MatchType.PRESENT, classLoader));
}

这里有两个方法值得注意,一个是 getCandidates 方法,一个是 getMatches 方法。首先通过 getCandidates 方法获取了 ConditionalOnClass 的 name 属性和 value 属性。然后通过 getMatches 方法将这些属性值进行比对,得到这些属性所指定的但在类加载器中不存在的类。如果发现类加载器中应该存在但事实上又不存在的类,则返回一个匹配失败的 Condition;反之,如果类加载器中存在对应类的话,则把匹配信息进行记录并返回一个 ConditionOutcome。

小结

至此整个SpringBoot自动配置的全部过程和基本原理已经讲完了,内容很多,整个流程总结一个图如下:

项目源码

github:https://github.com/dragon8844/springboot-learning/tree/main/java-spi

最后说一句

如果这篇文章对您有所帮助,或者有所启发的话,帮忙关注一下,您的支持是我坚持写作最大的动力,多谢支持。

此外,关注公众号:黑色的灯塔,专注Java后端技术分享,涵盖Spring,Spring Boot,SpringCloud,Docker,Kubernetes中间件等技术。

java spring 后端 springboot
阅读 98 更新于 2 月 25 日
赞1 收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
avatar
黑色的灯塔

Java开发工程师

9 声望
1 粉丝
关注作者
0 条评论
得票 时间
提交评论
avatar
黑色的灯塔

Java开发工程师

9 声望
1 粉丝
关注作者
宣传栏
目录

概览

上一节介绍了SpringBoot中的配置体系,接下来将会介绍SpringBoot的中最基础、最核心的自动装配(AutoConfiguration)机制。正是有了自动装配机制,我们可以很快的搭建一个Web项目,实现零配置,

看下SpringBoot是如何帮忙我们实现自动装配的,我们首先从@SpringBootApplication注解说起:

自动装配过程

@SpringBootApplication 启动类注解

每个SpringBoot项目都会有一个启动类,该类位于代码的根目录下,一般用XXXApplication命名,@SpringBootApplication注解会添加到该类上。@SpringBootApplication 注解位于 spring-boot-autoconfigure 工程的 org.springframework.boot.autoconfigure 包中,定义如下:

@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 {
 @AliasFor(annotation = EnableAutoConfiguration.class)
 Class<?>[] exclude() default {};
 
 @AliasFor(annotation = EnableAutoConfiguration.class)
 String[] excludeName() default {};
 
 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
 String[] scanBasePackages() default {};
 
 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
 Class<?>[] scanBasePackageClasses() default {};
}

该注解相比较显得有点复杂,从定义上看,@SpringBootApplication注解是一个组合注解,它是由三个注解组合而成,分别是:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

我们可以通过exclude 和 excludeName 属性来配置不需要实现自动装配的类或类名,也可以通过 scanBasePackages 和 scanBasePackageClasses 属性来配置需要进行扫描的包路径和类路径。接下来我们分别对这三个注解一一介绍:

@SpringBootConfiguration

@SpringBootConfiguration注解比较简单,定义如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
 @AliasFor(
 annotation = Configuration.class
 )
 boolean proxyBeanMethods() default true;
}

从定义来看,@SpringBootConfiguration等同于@Configuration注解,@Configuration比较常见,用来标识该类是JavaConfig配置类

@ComponentScan

@ComponentScan 注解不是 Spring Boot 引入的新注解,而是属于 Spring 容器管理的内容。@ComponentScan 注解就是扫描基于 @Component 等注解所标注的类所在包下的所有需要注入的类,并把相关 Bean 定义批量加载到容器中。

@EnableAutoConfiguration 自动配置注解

@EnableAutoConfiguration 这个注解一看名字就很牛,它是SpringBoot实现自动装配的核心注解,先看定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
​
 String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
​
 Class<?>[] exclude() default {};
​
 String[] excludeName() default {};
​
}

又是一个组合注解,是由@AutoConfigurationPackage和@Import(AutoConfigurationPackages.Registrar.class)组合成,下面分别来介绍:

@AutoConfigurationPackage 注解

@AutoConfigurationPackage注解定义如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
​
}

该注解的作用是将添加该注解的类所在的包作为自动装配的包路径进行管理。在@AutoConfigurationPackage注解的定义中,我们又发现了一个@Import注解,@Import 注解是由 Spring 提供的,作用是将某个类实例化并加入到 Spring IoC 容器中。所以我们要想知道 @Import(AutoConfigurationPackages.Registrar.class) 究竟做了什么就需要了解Registrar这个类里包含了哪些方法。

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
​
 @Override
 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
 register(registry, new PackageImport(metadata).getPackageName());
 }
​
 @Override
 public Set<Object> determineImports(AnnotationMetadata metadata) {
 return Collections.singleton(new PackageImport(metadata));
 }
​
}

Registrar类里一共有两个方法,分别是determineImports和registerBeanDefinitions,

new PackageImport(metadata).getPackageName()返回的就是@SpringBootApplication注解所在的类的包名

@Import(AutoConfigurationImportSelecor.class) 自动导入配置文件选择器

接下来我们回到@EnableAutoConfiguration注解中的 @Import(AutoConfigurationImportSelecor.class) 部分来,

这个类下有个重要的方法selectImports方法,Spring会把该方法返回的所有的类加载到IOC的容器中。所以这个类的主要作用是选择Spring加载哪些类到IOC容器中。

@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);
 
 //获取 configurations集合
 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 new AutoConfigurationEntry(configurations, exclusions);
 }

这段代码的核心是通过 getCandidateConfigurations 方法获取 configurations 集合并进行过滤。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;
}

这里我们重点关注下SpringFactoriesLoader.loadFactoryNames()方法,该方法会从META-INF/spring.factories文件中去查找自动配置的类,在这里,不得不提到JDK的SPI机制,因为无论从 SpringFactoriesLoader 这个类的命名上,还是 META-INF/spring.factories 这个文件目录,两者之间都存在很大的相通性。

Java SPI 机制和 SpringFactoriesLoader

要想理解 SpringFactoriesLoader 类,我们首先需要了解 JDK 中 SPI(Service Provider Interface,服务提供者接口)机制。简单的说SPI机制就是为接口寻找服务实现的机制,有点类似Spring的IOC的思想,不过将自动装配的控制权移到程序外部,可以避免程序中使用硬编码,在模块化设计中这种机制尤为重要。使用Java SPI机制需要以下几步:

当服务提供者提供了接口的一种具体实现后,在jar包的META-INF/services目录下创建一个以“接口全路径名”为命名的文件,内容为实现类的全路径名 当外部程序装配这个 jar 包时,就能通过该 jar 包 META-INF/services/ 目录中的配置文件找到具体的实现类名,并装载实例化,从而完成模块的注入

SpringFactoriesLoader 类似这种 SPI 机制,只不过以服务接口命名的文件是放在 META-INF/spring.factories 文件夹下,定义了一个key为EnableAutoConfiguration,SpringFactoriesLoader 会查找所有 META-INF/spring.factories 文件夹中的配置文件,并把 Key 为 EnableAutoConfiguration 所对应的配置项通过反射实例化为配置类并加载到容器中。

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 factoryTypeName = ((String) entry.getKey()).trim();
 for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
 result.add(factoryTypeName, factoryImplementationName.trim());
 }
 }
 }
 cache.put(classLoader, result);
 return result;
 }
 catch (IOException ex) {
 throw new IllegalArgumentException("Unable to load factories from location [" +
 FACTORIES_RESOURCE_LOCATION + "]", ex);
 }
}

以上就是SpringBoot中基于 @SpringBootApplication注解实现自动装配的基本过程和原理,不过SpringBoot默认情况下给我们提供了100多个AutoConfiguration的类

# Auto Configure
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.CloudServiceConnectorsAutoConfiguration,
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,

显然我们不可能把所有的类全部引入,所以在自动装配的时候,需要会根据类路径去寻找是否有对应的配置类,如果有的话还需要按照条件进行判断,来决定是否要装配,这里我要引出SpringBoot中经常要用到的另外一批注解@ConditionalOn系列注解。

@ConditionalOn 系列条件注解

当我们构建一个 Spring 应用的时候,有时我们想在满足指定条件的时候才将某个 bean 加载到应用上下文中, 在Spring 4.0 时代,我们可以通过 @Conditional 注解来实现这类操作,SpringBoot在 @Conditional注解的基础上进行了细化,定义了一系列的@ConditionalOn条件注解:

@ConditionalOnProperty:只有当所提供的属性属于 true 时才会实例化 Bean @ConditionalOnBean:只有在当前上下文中存在某个对象时才会实例化 Bean @ConditionalOnClass:只有当某个 Class 位于类路径上时才会实例化 Bean @ConditionalOnExpression:只有当表达式为 true 的时候才会实例化 Bean @ConditionalOnMissingBean:只有在当前上下文中不存在某个对象时才会实例化 Bean @ConditionalOnMissingClass:只有当某个 Class 在类路径上不存在的时候才会实例化 Bean @ConditionalOnNotWebApplication:只有当不是 Web 应用时才会实例化 Bean

由于@ConditionalOn 系列条件注解非常多,我们无意对所有这些组件进行展开。事实上这些注解的实现原理也大致相同,我们只需要深入了解其中一个就能做到触类旁通。这里我们挑选 @ConditionalOnClass 注解进行展开,该注解定义如下:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
  Class<?>[] value() default {};
  String[] name() default {};
}

可以看到, @ConditionalOnClass 注解本身带有两个属性,一个 Class 类型的 value,一个 String 类型的 name,所以我们可以采用这两种方式中的任意一种来使用该注解。同时 ConditionalOnClass 注解本身还带了一个 @Conditional(OnClassCondition.class) 注解。所以, ConditionalOnClass 注解的判断条件其实就包含在 OnClassCondition 这个类中。

OnClassCondition 是 SpringBootCondition 的子类,而 SpringBootCondition 又实现了Condition 接口。Condition 接口只有一个 matches 方法,如下所示:

@Override
public final boolean matches(ConditionContext context,
            AnnotatedTypeMetadata metadata) {
        String classOrMethodName = getClassOrMethodName(metadata);
        try {
            ConditionOutcome outcome = getMatchOutcome(context, metadata);
            logOutcome(classOrMethodName, outcome);
            recordEvaluation(context, classOrMethodName, outcome);
            return outcome.isMatch();
        }
        //省略其他方法
}

这里的 getClassOrMethodName 方法获取被添加了@ConditionalOnClass 注解的类或者方法的名称,而 getMatchOutcome 方法用于获取匹配的输出。我们看到 getMatchOutcome 方法实际上是一个抽象方法,需要交由 SpringBootCondition 的各个子类完成实现,这里的子类就是 OnClassCondition 类。在理解 OnClassCondition 时,我们需要明白在 Spring Boot 中,@ConditionalOnClass 或者 @ConditionalOnMissingClass 注解对应的条件类都是 OnClassCondition,所以在 OnClassCondition 的 getMatchOutcome 中会同时处理两种情况。这里我们挑选处理 @ConditionalOnClass 注解的代码,核心逻辑如下所示:

ClassLoader classLoader = context.getClassLoader();
ConditionMessage matchMessage = ConditionMessage.empty();
List<String> onClasses = getCandidates(metadata, ConditionalOnClass.class);
if (onClasses != null) {
            List<String> missing = getMatches(onClasses, MatchType.MISSING, classLoader);
            if (!missing.isEmpty()) {
                return ConditionOutcome
                        .noMatch(ConditionMessage.forCondition(ConditionalOnClass.class)
                                .didNotFind("required class", "required classes")
                                .items(Style.QUOTE, missing));
            }
            matchMessage = matchMessage.andCondition(ConditionalOnClass.class)
                    .found("required class", "required classes").items(Style.QUOTE, getMatches(onClasses, MatchType.PRESENT, classLoader));
}

这里有两个方法值得注意,一个是 getCandidates 方法,一个是 getMatches 方法。首先通过 getCandidates 方法获取了 ConditionalOnClass 的 name 属性和 value 属性。然后通过 getMatches 方法将这些属性值进行比对,得到这些属性所指定的但在类加载器中不存在的类。如果发现类加载器中应该存在但事实上又不存在的类,则返回一个匹配失败的 Condition;反之,如果类加载器中存在对应类的话,则把匹配信息进行记录并返回一个 ConditionOutcome。

小结

至此整个SpringBoot自动配置的全部过程和基本原理已经讲完了,内容很多,整个流程总结一个图如下:

项目源码

github:https://github.com/dragon8844/springboot-learning/tree/main/java-spi

最后说一句

如果这篇文章对您有所帮助,或者有所启发的话,帮忙关注一下,您的支持是我坚持写作最大的动力,多谢支持。

此外,关注公众号:黑色的灯塔,专注Java后端技术分享,涵盖Spring,Spring Boot,SpringCloud,Docker,Kubernetes中间件等技术。