springboot 2.0 自动装配原理 以redis为例

 

当面试管问你springboot 和 普通spring 有什么区别?

  您还在回答: 简化了配置 ,内置tomcat 等等 吗 ? 那只是皮毛, 最重要的还是自动化配置。一起来了解一下

第一步:

第二步:

第三步:

第四步:

public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {
    private static final String[] NO_IMPORTS = new String[0];
    private static final Log logger = LogFactory.getLog(AutoConfigurationImportSelector.class);
    private static final String PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude";
    private ConfigurableListableBeanFactory beanFactory;
    private Environment environment;
    private ClassLoader beanClassLoader;
    private ResourceLoader resourceLoader;

    public AutoConfigurationImportSelector() {
    }

    public String[] selectImports(AnnotationMetadata annotationMetadata) {  //很重要
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);

       //去mata-info/spring.factories文件中 查询 EnableAutoConfiguration对应得所有类 一共110个
            List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
    //去除重复的配置类,若我们自己写的starter 可能存主重复的
            configurations = this.removeDuplicates(configurations);
            Set exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
    //根据maven 包导入的启动器过滤出 需要导入的配置类
            configurations = this.filter(configurations, autoConfigurationMetadata);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return StringUtils.toStringArray(configurations);
        }
    }

  

  

 

 第五步:

 

 

 xml 版配置:

 

 

整个springboot 自动配置得流程用以下得图来概况:

 

转载于:https://www.cnblogs.com/smallFishs/p/10651019.html

你可能感兴趣的:(springboot 2.0 自动装配原理 以redis为例)