@EnableAutoConfiguration源码详解

2020-12-28  本文已影响0人  梦想做一个不秃头的程序猿

  @EnableAutoConfiguration一看名字就知道是激活自动装配的,那它是如何激活自动装配的呢,这一切要在其源码实现层面上寻找答案。

@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 {};
}
public String[] selectImports(AnnotationMetadata annotationMetadata) {
        // 如果未开启自动装配就直接放回空数组
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            // 获取所有符合要求的配置类以及需要排除的类的全类名
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
    }

protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
       // 如果未开启自动配置直接返回一个空对象
        if (!this.isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        } else {
            // 获取标注类元信息
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
            //加载需要自动装配的候选类名集合
            List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
           // 去重
            configurations = this.removeDuplicates(configurations);
            // 排除需要排除的配置类
            Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            // 通过过滤器盘判断是否引相关组件,如果有则进行相关配置
            configurations = this.getConfigurationClassFilter().filter(configurations);
            // 派发事件
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
           // 返回最终需要托管的类
            return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
        }
    }
  1. AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
  2. List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
    其中configurations接收getCandidateConfigurations方法的返回值,获取候选类名集合。@EnableAutoConfiguration注解所在的spring-boot-autoconfigure包,是spring boot的核心模块,提供了大量的内建自动装配@Configuration类,同时这些类均配置在META-INF/spring.factories。而变量configurations 获取的就是自动加载的META-INF/spring.factories中的名为“org.springframework.boot.autoconfigure.EnableAutoConfiguration”的属性值,这里面就是目前spring boot的所有自动配置类。
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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方法作为结果返回,之后再经过空值检验,最终赋值给configurations。

  1. configurations = this.removeDuplicates(configurations);
    之后removeDuplicates函数利用set去重
protected final <T> List<T> removeDuplicates(List<T> list) {
        return new ArrayList(new LinkedHashSet(list));
    }
  1. Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
    getExclusions方法获得一个自动装配的类的排除名单。标注@EnableAutoConfiguration配置类的注解属性exclude和excludeName,以及spring.autoconfigure.exclude配置值。
protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        Set<String> excluded = new LinkedHashSet();
        excluded.addAll(this.asList(attributes, "exclude"));
        excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
        excluded.addAll(this.getExcludeAutoConfigurationsProperty());
        return excluded;
    }
  1. this.checkExcludedClasses(configurations, exclusions);
    checkExcludedClasses函数判断排除类名集合是否合法,如果存在排除类,这个类存在于当前ClassLoader并且不存在与自动装配候选名单,则会抛出IllegalStateException异常。
private void checkExcludedClasses(List<String> configurations, Set<String> exclusions) {
        List<String> invalidExcludes = new ArrayList(exclusions.size());
        Iterator var4 = exclusions.iterator();

        while(var4.hasNext()) {
            String exclusion = (String)var4.next();
            if (ClassUtils.isPresent(exclusion, this.getClass().getClassLoader()) && !configurations.contains(exclusion)) {
                invalidExcludes.add(exclusion);
            }
        }

        if (!invalidExcludes.isEmpty()) {
            this.handleInvalidExcludes(invalidExcludes);
        }

    }
protected void handleInvalidExcludes(List<String> invalidExcludes) {
        StringBuilder message = new StringBuilder();
        Iterator var3 = invalidExcludes.iterator();

        while(var3.hasNext()) {
            String exclude = (String)var3.next();
            message.append("\t- ").append(exclude).append(String.format("%n"));
        }

        throw new IllegalStateException(String.format("The following classes could not be excluded because they are not auto-configuration classes:%n%s", message));
    }
  1. configurations.removeAll(exclusions)
    将exclusions集合中的元素从configurations集合中删除。
  2. configurations = this.getConfigurationClassFilter().filter(configurations);
    删除了排除类名后,还需要再次过滤,过滤掉那些spring.factories中当前ClassLoader不存在的类。
    其中,AutoConfigurationImportFilter对象集合被SpringFactoriesLoader.loadFactorie根据spring.factories中的配置加载。

org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=
org.springframework.boot.autoconfigure.condition.OnBeanCondition,
org.springframework.boot.autoconfigure.condition.OnClassCondition,
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition

List<String> filter(List<String> configurations) {
            long startTime = System.nanoTime();
            String[] candidates = StringUtils.toStringArray(configurations);
            boolean skipped = false;
            Iterator var6 = this.filters.iterator();

            int i;
            while(var6.hasNext()) {
                AutoConfigurationImportFilter filter = (AutoConfigurationImportFilter)var6.next();
                boolean[] match = filter.match(candidates, this.autoConfigurationMetadata);

                for(i = 0; i < match.length; ++i) {
                    if (!match[i]) {
                        candidates[i] = null;
                        skipped = true;
                    }
                }
            }

            if (!skipped) {
                return configurations;
            } else {
                List<String> result = new ArrayList(candidates.length);
                String[] var12 = candidates;
                int var14 = candidates.length;

                for(i = 0; i < var14; ++i) {
                    String candidate = var12[i];
                    if (candidate != null) {
                        result.add(candidate);
                    }
                }

                if (AutoConfigurationImportSelector.logger.isTraceEnabled()) {
                    int numberFiltered = configurations.size() - result.size();
                    AutoConfigurationImportSelector.logger.trace("Filtered " + numberFiltered + " auto configuration class in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
                }

                return result;
            }
        }
  1. this.fireAutoConfigurationImportEvents(configurations, exclusions);
    fireAutoConfigurationImportEvents进行事件派发,派发AutoConfigurationImportEvent事件,
private void fireAutoConfigurationImportEvents(List<String> configurations, Set<String> exclusions) {
        // 
        List<AutoConfigurationImportListener> listeners = this.getAutoConfigurationImportListeners();
        if (!listeners.isEmpty()) {
            AutoConfigurationImportEvent event = new AutoConfigurationImportEvent(this, configurations, exclusions);
            Iterator var5 = listeners.iterator();

            while(var5.hasNext()) {
                AutoConfigurationImportListener listener = (AutoConfigurationImportListener)var5.next();
                this.invokeAwareMethods(listener);
                listener.onAutoConfigurationImportEvent(event);
            }
        }

    }

AutoConfigurationImportListener实例同样由SpringFactoriesLoader.loadFactories加载:

protected List<AutoConfigurationImportListener> getAutoConfigurationImportListeners() {
        return SpringFactoriesLoader.loadFactories(AutoConfigurationImportListener.class, this.beanClassLoader);
    }

org.springframework.boot.autoconfigure.AutoConfigurationImportListener=
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
ConditionEvaluationReportAutoConfigurationImportListener为内建实现,用于记录自动装配的条件评估报告。

  1. return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);




    \color{#ff1492}{@ AutoConfigurationPackage}
      spring boot 1.3之后引入@AutoConfigurationPackage注解用来代替原来的@Import({ EnableAutoConfigurationImportSelector.class, AutoConfigurationPackages.Register.class }),这个注解实现了将注解所在类所造的package作为自动装配package进行管理。在spring boot 2.4.1中AutoConfigurationPackage源码如下所示:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
    String[] basePackages() default {};

    Class<?>[] basePackageClasses() default {};
}

其中内部类Register类是自动装配BasePackages的核心逻辑,是接口ImportBeanDefinitionRegister、DeterminableImports 的实现。

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
        Registrar() {
        }

        public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
            AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
        }

        public Set<Object> determineImports(AnnotationMetadata metadata) {
            return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
        }
    }

registerBeanDefinitions调用AutoConfigurationPackages.register方法标注当前标注类所在的package。由于@EnableAutoConfiguration可能同时被不同的class标注,因此AutoConfigurationPackages.Register可能被Import多次,当第一次被加载时,直接将BasePackagesBeanDefinition注册到spring应用上下文,之后,如果再次加载,就直接将其添加到已有集合中。

public static void register(BeanDefinitionRegistry registry, String... packageNames) {
        if (registry.containsBeanDefinition(BEAN)) {
            AutoConfigurationPackages.BasePackagesBeanDefinition beanDefinition = (AutoConfigurationPackages.BasePackagesBeanDefinition)registry.getBeanDefinition(BEAN);
            beanDefinition.addBasePackages(packageNames);
        } else {
            registry.registerBeanDefinition(BEAN, new AutoConfigurationPackages.BasePackagesBeanDefinition(packageNames));
        }
}

static final class BasePackagesBeanDefinition extends GenericBeanDefinition {
        private final Set<String> basePackages = new LinkedHashSet();

        BasePackagesBeanDefinition(String... basePackages) {
            this.setBeanClass(AutoConfigurationPackages.BasePackages.class);
            this.setRole(2);
            this.addBasePackages(basePackages);
        }

        public Supplier<?> getInstanceSupplier() {
            return () -> {
                return new AutoConfigurationPackages.BasePackages(StringUtils.toStringArray(this.basePackages));
            };
        }

        private void addBasePackages(String[] additionalBasePackages) {
            this.basePackages.addAll(Arrays.asList(additionalBasePackages));
        }
 }
上一篇下一篇

猜你喜欢

热点阅读