实际上,Spring Boot的自动装配是模块装配+条件装配+SPI机制的组合使用,而这一切都凝聚在Spring Boot主启动类的@SpringBootApplication注解上。
@SpringBootApplication注解是由三个注解组合而成的复合注解:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication
因此,主启动类标注@SpringBootApplication,会触发@EnableAutoConfiguration自动装配和@ComponentScan组件扫描。
@ComponentScan放在@SpringBootApplication中的意图:扫描主启动类所在包及其子包下的所有组件。
这也解释了为什么主启动类要放到所有类所在包的最外层。
下面通过一个例子加以理解:
(1)创建Boss类和BossConfiguration类,两个类放在同一包下
@Component("aBoss")
public class Boss {
}
@Configuration
@ComponentScan
public class BossConfiguration {
}
通过@ComponentScan注解,SpringBoot会扫描BossConfiguration所在包及其子包的所有组件。由于Boss组件和BossConfiguration组件在同一包下,所以Boss组件将会组测到IOC容器中。
(2)测试
public class BossApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BossConfiguration.class);
System.out.println("-------分割线-------");
Stream.of(context.getBeanDefinitionNames()).forEach(System.out::println);
System.out.println("-------分割线-------");
}
}
输出结果(已省略一些内部组件打印,下同):
-------分割线-------
bossConfiguration
aBoss
-------分割线-------
(3)Boss类和BossConfiguration类放在同一个包下肯定是不合理的,所以调整一下位置,把BossConfiguration来放在其它目录下,如图:
此时的输出结果显示,Boss并没有注册到IOC容器:
-------分割线-------
bossConfiguration
-------分割线-------
(4)查看@ComponentScan的源码,可以找到几个常用的属性:
在@ComponentScan加basePackages参数,指定Boss所在的包:
@Configuration
@ComponentScan(basePackages = "com.star.springboot.assemble.test01.pojo")
public class BossConfiguration {
}
此时的输出结果显示,Boss已经注册到IOC容器:
-------分割线-------
bossConfiguration
aBoss
-------分割线-------
和平时使用不一样,@ComponentScan注解额外添加了两个过滤条件:
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
它的作用是,让开发者向IOC容器注册自定义的过滤器组件,在组件扫描过程中匹配自定义过滤器的规则,以过滤掉一些不需要的组件。
javadoc:
Implementations should provide a subclass registered with {@link BeanFactory} and override the {@link #match(MetadataReader, MetadataReaderFactory)} method.
TypeExcludeFilter的使用方法:在BeanFactory中注册一个子类,并重写match方法,SpringBoot会自动找到这些子类并调用它们。
【拓展】由FilterType源码可知,@ComponentScan不仅支持自定义过滤器组件(CUSTOM),还支持以注解(ANNOTATION)、类(ASSIGNABLE_TYPE)、AspectJ(ASPECTJ)、正则(REGEX)为线索进行过滤。
public enum FilterType {
/**
* Filter candidates marked with a given annotation.
* @see org.springframework.core.type.filter.AnnotationTypeFilter
*/
ANNOTATION,
/**
* Filter candidates assignable to a given type.
* @see org.springframework.core.type.filter.AssignableTypeFilter
*/
ASSIGNABLE_TYPE,
/**
* Filter candidates matching a given AspectJ type pattern expression.
* @see org.springframework.core.type.filter.AspectJTypeFilter
*/
ASPECTJ,
/**
* Filter candidates matching a given regex pattern.
* @see org.springframework.core.type.filter.RegexPatternTypeFilter
*/
REGEX,
/** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM
}
下面继续通过上面的例子加以理解:
(1)注册自定义的过滤器组件:在上下文注册TypeExcludeFilter的子类,并重写match方法,match方法返回true则匹配,返回false则不匹配。SpringBoot会自动找到这些TypeExcludeFilter的子类并执行match方法。
public class MyTypeExcludeFilter extends TypeExcludeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
System.out.println("自定义过滤器执行了...");
return Boss.class.getName().equals(metadataReader.getClassMetadata().getClassName());
}
}
(2)在@ComponentScan配置自定义过滤器组件
@Configuration
@ComponentScan(basePackages = "com.star.springboot.assemble.test01.pojo",
excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeExcludeFilter.class)})
public class BossConfiguration {
}
(3)测试
-------分割线-------
bossConfiguration
-------分割线-------
输出结果显示,Boss没有注册到IOC容器,说明自定义的过滤器组件生效了。
(4)TypeExcludeFilter的核心逻辑
TypeExcludeFilter的底层实现:从BeanFactory中提取出所有类为TypeExcludeFilter的Bean,并缓存到本地(空间换时间的设计体现),后续在进行组件扫描时会依次调用这些TypeExcludeFilter对象,检查被扫描的类是否符合匹配规则。
符合规则的,不注册到IOC容器;不符合的才注册到IOC容器。
public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {
private BeanFactory beanFactory;
private Collection<TypeExcludeFilter> delegates;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) {
// 提取出所有TypeExcludeFilter并依次调用其match方法检查是否匹配
for (TypeExcludeFilter delegate : getDelegates()) {
if (delegate.match(metadataReader, metadataReaderFactory)) {
return true;
}
}
}
return false;
}
private Collection<TypeExcludeFilter> getDelegates() {
Collection<TypeExcludeFilter> delegates = this.delegates;
if (delegates == null) {
// 从BeanFactory中提取所有类为TypeExcludeFilter的Bean
delegates = ((ListableBeanFactory) this.beanFactory).getBeansOfType(TypeExcludeFilter.class).values();
this.delegates = delegates;
}
return delegates;
}
// ...
}
public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware {
private ClassLoader beanClassLoader;
private volatile List<String> autoConfigurations;
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader;
}
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader);
}
private boolean isConfiguration(MetadataReader metadataReader) {
// 检查是配置类的规则:是否被@Configuration注解修饰
return metadataReader.getAnnotationMetadata().isAnnotated(Configuration.class.getName());
}
private boolean isAutoConfiguration(MetadataReader metadataReader) {
// 检查是自动配置类的规则:是否被定义在spring.factories中的EnableAutoConfiguration中
return getAutoConfigurations().contains(metadataReader.getClassMetadata().getClassName());
}
protected List<String> getAutoConfigurations() {
if (this.autoConfigurations == null) {
this.autoConfigurations = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader);
}
return this.autoConfigurations;
}
}
@SpringBootConfiguration其实很简单,本身仅组合了一个Spring Framework的@Configuration而已。换句话说,一个类标注了@SpringBootConfiguration,也就是标注了@Configuration,代表这个类是一个注解配置类。
@Configuration
public @interface SpringBootConfiguration
@EnableAutoConfiguration是重头戏,承载了SpringBoot自动装配的灵魂。
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, if you have tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean). When using @SpringBootApplication, the auto-configuration of the context is automatically enabled and adding this annotation has therefore no additional effect.
第一段讲解注解本身的作用。
标注@EnableAutoConfiguration后,Spring上下文的自动装配机制将会启用,SpringBoot会尝试猜测和配置当前项目可能需要的Bean。自动配置类的使用通常基于项目类路径和定义的Bean。例如,如果在类路径下引用了tomcat-embedded.jar包,则SpringBoot会猜测当前项目很可能需要配置一个TomcatServletWebServerFactory(除非项目已经手动配置了一个ServletWebServerFactory)。标注@SpringBootApplication后,Spring上下文的自动装配会自动启用,所以再配置@EnableAutoConfiguration没有附加作用(也就是不需要再配置@EnableAutoConfiguration)。Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration. You can always manually exclude() any configuration that you never want to apply (use excludeName() if you don’t have access to them). You can also exclude them via the spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans have been registered.
第二段讲解SpringBoot自动装配的机制和禁用方法。
SpringBoot自动装配会尽可能地智能化,并会在项目注册了更多自定义配置时自动退出(即被覆盖)。开发者可以手动排除任何不想使用的自动配置(使用exclude属性,或在无法访问时使用excludeName属性),还可以通过全局配置文件的spring.autoconfigure.exclude属性。自动配置总是在用户自定义的Bean被注册之后才进行。The package of the class that is annotated with @EnableAutoConfiguration, usually via @SpringBootApplication, has specific significance and is often used as a ‘default’. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration (if you’re not using @SpringBootApplication) in a root package so that all sub-packages and classes can be searched.
第三段讲解组件扫描的规则。
被@EnableAutoConfiguration或@SpringBootApplication标注的类,其所在包有特殊的含义,通常被定义为“默认值”。例如,当扫描被@Entity标注的类时会用到。通常建议,把@EnableAutoConfiguration标注在一个根包中的类,这个根包及其子包的所有组件都将被扫描。Auto-configuration classes are regular Spring @Configuration beans. They are located using the SpringFactoriesLoader mechanism (keyed against this class). Generally auto-configuration beans are @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissingBean annotations).
第四段讲解自动配置类与SPI机制的关系。
自动配置类本身也是常规的Spring配置类,只不过它们的加载是通过SpringFactoriesLoader机制(即SPI机制)。通常自动配置类也是条件配置类(被@Conditional系列注解标注,最常用的是@ConditionalOnClass和@ConditionalOnMissingBean注解)。
总结:标注@EnableAutoConfiguration后,会启用SpringBoot的自动装配,根据导入的依赖、上下文配置合理加载默认的自动配置。
@EnableAutoConfiguration本身也是一个组合注解,包含了两个注解:
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage
由以上源码可知,@AutoConfigurationPackage注解组合了一个@Import注解,导入了一个内部类AutoConfigurationPackages.Registrar。
@AutoConfigurationPackage的作用是,将主启动类所在的包记录下来,注册到AutoConfigurationPackages中。
在SpringBoot 2.3.0版本以前,@AutoConfigurationPackage注解没有任何属性,标注了该注解即确定了主启动类所在包(约定)。在SpringBoot 2.3.0版本以后,注解中多了两个属性,basePackages和basePackageClasses,它们可以手动指定应用的根包/根路径(配置)。如果没有手动指定,则仍然采用默认的主启动类所在包(约定大于配置)。
/**
* {@link ImportBeanDefinitionRegistrar} to store the base package from the importing configuration.
*/
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));
}
@Override
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new PackageImports(metadata));
}
}
由以上源码可知,Registrar本身是一个ImportBeanDefinitionRegistrar,以编程式配置向IOC容器注册bean对象,而这个对象正是主启动类的包路径(方法注释中说到:store the base package)。
再来关注一下registerBeanDefinitions方法,该方法直接调用register方法,第二个参数是由PackageImports导出的一个包名数组。
PackageImports(AnnotationMetadata metadata) {
AnnotationAttributes attributes = AnnotationAttributes
.fromMap(metadata.getAnnotationAttributes(AutoConfigurationPackage.class.getName(), false));
List<String> packageNames = new ArrayList<>();
// 先提取自定义的basePackages属性
for (String basePackage : attributes.getStringArray("basePackages")) {
packageNames.add(basePackage);
}
// 再提取自定义的basePackageClasses属性
for (Class<?> basePackageClass : attributes.getClassArray("basePackageClasses")) {
packageNames.add(basePackageClass.getPackage().getName());
}
// 如果都没有提取到,则提取默认的,即注解所标注的类所在包,也就是主启动类所在包
if (packageNames.isEmpty()) {
packageNames.add(ClassUtils.getPackageName(metadata.getClassName()));
}
this.packageNames = Collections.unmodifiableList(packageNames);
}
PackageImports的作用就是提取应用根包名,体现了约定大于配置。提取出根包路径后,调用外层的register方法。
private static final String BEAN = AutoConfigurationPackages.class.getName();
public static void register(BeanDefinitionRegistry registry, String... packageNames) {
// 检查IOC容器中是否有AutoConfigurationPackages这个Bean
if (registry.containsBeanDefinition(BEAN)) {
// 有这个Bean时,拿到构造方法,把根包路径packageNames作为参数传进去
BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
constructorArguments.addIndexedArgumentValue(0, addBasePackages(constructorArguments, packageNames));
}
else {
// 没有这个Bean时,则创建这个Bean并注册到IOC容器中,把根包路径packageNames作为构造方法的参数传进去
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(BasePackages.class);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(BEAN, beanDefinition);
}
}
由以上源码可知,这两个分支逻辑的共性在于:添加构造方法的参数。这个参数就是PackageImports提取出来的根包路径packageNames。
那根包路径保存到哪里了?从else分支可以看到,实际上构造了一个BasePackages对象,内部维护了一个字符串数组以存放这些根包路径。
/**
* Holder for the base package (name may be null to indicate no scanning).
*/
static final class BasePackages {
private final List<String> packages;
BasePackages(String... names) {
List<String> packages = new ArrayList<>();
for (String name : names) {
if (StringUtils.hasText(name)) {
packages.add(name);
}
}
this.packages = packages;
}
// ...
}
最后一个问题,保存SpringBoot应用的根包路径用来做什么?
SpringBoot可以方便地整合第三方技术。以MyBatis为例,当项目引入mybatis-spring-boot-starter依赖后,有一个MybatisAutoConfiguration类,其中有一个AutoConfiguredMapperScannerRegistrar组件。
public static class AutoConfiguredMapperScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar {
private BeanFactory beanFactory;
public AutoConfiguredMapperScannerRegistrar() {
}
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// ...
// 获取应用的根包路径
List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
// ...
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
builder.addPropertyValue("processPropertyPlaceHolders", true);
builder.addPropertyValue("annotationClass", Mapper.class);
// 放置到组件构造器中
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages));
// ...
registry.registerBeanDefinition(MapperScannerConfigurer.class.getName(), builder.getBeanDefinition());
}
}
// ...
}
由以上的源码可以,AutoConfiguredMapperScannerRegistrar是一个ImportBeanDefinitionRegistrar,用于向IOC容器注册MapperScannerConfigurer组件,用于Mapper接口扫描。在创建MapperScannerConfigurer组件时,应用的根包路径作为其中一个参数被设置进去,由此也就体现出basePackages的作用。
通过以上的分析,也就理解了SpringBoot的主启动类要在所有类的最外层。
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered
由源码可知,AutoConfigurationImportSelector本身是一个DeferredImportSelector。
在 SpringBoot源码解读与原理分析(二)模块装配 中提到过,DeferredImportSelector除了具备ImportSelector的作用,它的执行时机也比ImportSelector更晚,所以更适合做一些补充性的工作。同时它还添加了分组的概念。
自动装配的设计是约定大于配置,项目中已经有的配置不会再重复注册,项目中没有配置的部分会予以补充,而负责补充的任务是交给自动配置类的,AutoConfigurationImportSelector就是加载这些自动配置类。
DeferredImportSelector中真正起作用的方法是getImportGroup方法:
@Override
public Class<? extends Group> getImportGroup() {
return AutoConfigurationGroup.class;
}
AutoConfigurationGroup实现了DeferredImportSelector.Group接口,这个接口定义了一个process方法,这个方法才是真正负责加载所有自动配置类的入口。
private final Map<String, AnnotationMetadata> entries = new LinkedHashMap<>();
private final List<AutoConfigurationEntry> autoConfigurationEntries = new ArrayList<>();
@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()));
// 核心逻辑:getAutoConfigurationEntry加载自动配置类
AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
.getAutoConfigurationEntry(annotationMetadata);
// 将加载到的自动配置类存入缓存中(一个Map集合、一个List集合)
this.autoConfigurationEntries.add(autoConfigurationEntry);
for (String importClassName : autoConfigurationEntry.getConfigurations()) {
this.entries.putIfAbsent(importClassName, annotationMetadata);
}
}
加载自动配置类的核心方法是getAutoConfigurationEntry,加载完成后,将加载到的自动配置类存入缓存中(一个Map集合、一个List集合)。
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);
// 广播AutoConfigurationImportEvent事件
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
主干逻辑:加载自动配置类→自动配置类去重→获取并移除显式配置了要排除的自动配置类→封装Entry返回。
这里加载自动配置类的方法就是利用SpringFramework的SPI机制,从spring.factories中提取出所有配置了@EnableAutoConfiguration的自动配置类:
/**
* Return the auto-configuration class names that should be considered. By default
* this method will load candidates using {@link SpringFactoriesLoader} with
*/
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
// 提取配置了@EnableAutoConfiguration的自动配置类
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
// 断言是否正确获得自动配置类,提示在 META-INF/spring.factories
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;
}
获取显式配置了要排除的自动配置类有三种方式:@SpringBootApplication或@EnableAutoConfiguration注解的exclude、excludeName属性,以及全局配置文件的spring.autoconfigure.exclude属性。
/**
* Return any exclusions that limit the candidate configurations.
*/
protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
Set<String> excluded = new LinkedHashSet<>();
// exclude属性
excluded.addAll(asList(attributes, "exclude"));
// excludeName属性
excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
// spring.autoconfigure.exclude属性
excluded.addAll(getExcludeAutoConfigurationsProperty());
return excluded;
}
至此,需要被加载的自动配置类全部收集完毕,并返回这些自动配置类的全限定名,存入AutoConfigurationGroup的缓存中,后续IOC容器会取出这些自动配置类并解析,完成自动配置类的装载。
总结一下SpringBoot的核心@SpringBootApplication和自动装机制。
…
本节完,更多内容请查阅分类专栏:SpringBoot源码解读与原理分析