zoukankan      html  css  js  c++  java
  • spring boot (3.自动配置@enableautoconfiguration)

     

     

    主入口类/启动类

    @SpringBootApplication

    启动类是一个组合注解

    配置类也是容器中的一个组件

    第一个注解@springbootconfigration

     

      .

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration    springboot定义的配置注解
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    public @interface SpringBootApplication {
    

      ->configration

    .

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration   spring定义的配置类注解
    public @interface SpringBootConfiguration {
    

       

    第二个注解@enableautoconfiguration开启自动配置功能

    告诉s.b.开启自动配置功能

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage  自动配置包,将SpringBootApplication主配置类所在包以及子包的所有子类扫描到spring容器
    @Import({AutoConfigurationImportSelector.class})  导入组件开启自动配置类导包的选择器
    public @interface EnableAutoConfiguration {
    

      

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import({Registrar.class})  spring的底层注解,给容器中导入一个组件
    public @interface AutoConfigurationPackage {
    }
    

      

    假如在主配置类所在包package com.atguigu;外面package com;定义一个路径/hello就扫描不进来了

    @Import({AutoConfigurationImportSelector.class})将所有需要导入的组件以全类名的方式返回;这些组件被添加到容器中;最终给容器导入很多自动配置类(XXAutoConfiguration);给容器中导入这个场景需要的所有组件,并配置好这些组件;

    有了自动配置类免去了手动编写配置和注入功能组件的工作

                AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
                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.filter(configurations, autoConfigurationMetadata);
                this.fireAutoConfigurationImportEvents(configurations, exclusions);
                return StringUtils.toStringArray(configurations);
    

      

    打断点可以看到如下内容

    进入getCandidateConfigurations方法

     

    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    第一个参数:
    protected Class<?> getSpringFactoriesLoaderFactoryClass() {
    return EnableAutoConfiguration.class;
    }
    SpringFactoriesLoader.loadFactoryNames()
    SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader)
    

      从类加载器中获取一个资源,把资源当成properties文件,然后拿出工厂的名字。

    springboot在启动的时候从META-INF/spring.factories获取EnableAutoConfiguration指定的值

    将这些值作为自动配置类 导入到容器中,自动配置类就生效,帮我们进行自动配置工作

     进入扩展

    想找到什么配置,都可以在扩展中meta-inf,spring.factories中这样找

    比如选择其中的webmvcautoconfiguration,看到给容器中添加一个filter组件

    比如添加解析器

     一揽子解决方案全都自动配置

    整体的解决方案自动配置都在这里

    D:Maven-Repositoryorgspringframeworkootspring-boot-autoconfigure2.0.2.RELEASEspring-boot-autoconfigure-2.0.2.RELEASE.jar!orgspringframeworkootautoconfigure

     每一种怎么配的都在这里学习,也可以在其中改善

    
    
  • 相关阅读:
    计算机中的二进制运算
    面试题14:剪绳子
    面试题13:机器人的运动范围
    面试题12:矩阵中的路径
    面试题11:旋转数组的最小数字
    面试题10_2:跳台阶
    面试题10:斐波那契数列
    HDU 2202 最大三角形(凸包)
    刚装的系统C盘占空间特别大怎么办?关闭win7的系统还原和调整虚拟内存
    POJ 1113 Wall (凸包)
  • 原文地址:https://www.cnblogs.com/tabCtrlShift/p/9069679.html
Copyright © 2011-2022 走看看