• SpringBoot原理篇


    一、SpringBoot的两大特性

    1. 依赖管理

    • SpringBoot2使用父项目做依赖管理,我们的每个Spring项目都依赖于spring-boot-starter-parent,而spring-boot-starter-parent又依赖于spring-boot-dependenciesspring-boot-dependencies中就声明了几乎所有开发中常用的依赖的版本号,因此,我们在创建项目时,无需关注版本号,会进行自动版本仲裁,引入的依赖都可以不写版本,当然,如果我们想引入非版本仲裁的jar,可以选择直接在当前项目的pom.xml中利用标签来修改依赖的版本,也可以选择在引入时直接在当前项目的pom.xml文件添加上
    • 我们的需要的版本号SpringBoot2采用了stater场景启动器,它是一组依赖的集合描述,当我们需要引入新的场景时,我们只需要引入它的starter,那么这个场景的所有常规需要的依赖我们都自动引入,所有的官方提供的starter命名方式都为spring-boot-starter-*,第三方的starter组件命名方式都为*-spring-boot-starter,所有场景启动器最底层的依赖都是 spring-boot-starter

    2. 自动配置

    2.1 配置内容

    1. 自动引入Tomcat依赖并配置好Tomcat。
    2. 自动配好SpringMVC的场景并配置全套组件。
    3. 自动配置好了包扫描机制(主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来,依靠@SpringBootApplication注解)。
    4. 自动将配置文件中的内容映射到某个java类中,并为其配置默认值(依靠@ConfigurationProperties注解)。
    5. 按需加载所有自动配置项,所有自动配置功能都在spring-boot-autoconfigure中,只有引入的场景自动配置才会开启(依靠@Conditional注解)。
    6. 等等等…

    2.2 配置原理

    1. 首先分析SpringBoot的主启动类上的@SpringBootApplicaion 注解
      @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}
      )}
      )
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      重点分析其中的@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan注解。
    2. @SpringBootConfiguration注解
      @Target(ElementType.TYPE)
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Configuration
      public @interface SpringBootConfiguration {
      	@AliasFor(
      	annotation = Configuration.class
      	)
      	boolean proxyBeanMethods() default true;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      @Configuration代表当前的类是一个配置类。
    3. @ComponentScan:指定扫描哪些包。
    4. @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 {};
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      重点分析@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)
    5. @AutoConfigurationPackage:自动配置包,指定了默认的包规则。
      	@Target(ElementType.TYPE)
      	@Retention(RetentionPolicy.RUNTIME)
      	@Documented
      	@Inherited
      	@Import(AutoConfigurationPackages.Registrar.class)
      	public @interface AutoConfigurationPackage {
      		String[] basePackages() default {};
      		Class<?>[] basePackageClasses() default {};
      	}
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 首先,该注解给容器中导入了Registrar组件;
      • 随后我们利用Registrat中的方法,获取当前注解标注的类的包名,并将该包下所有组件导入。
      • 注解:该注解标注的类是MainApplication,所以所在包名就是主类所在的包,也正是因此而有的自动扫描包的功能。
    6. @Import(AutoConfigurationImportSelector.class):利用AutoConfigurationImportSelector.class类下的getAutoConfigurationEntry(annotationMetadata)获取需要导入的组件名称,然后进行组件的批量导入
      • getAutoConfigurationEntry中先调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置名称,然后对其进行筛选后返回。
      • getCandidateConfigurations方法内部利用工厂加载Map> loadSpringFactories(@Nullable ClassLoaderclassLoader);得到所有的组件名称并返回。
      • loadSpringFactories方法内部其实就是加载META-INF/spring.factories位置的文件,默认会扫描们当前系统里面所有META-INF/spring.factories位置的文件,spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories,该文件中就写了所有了spring-boot一启动就要给容器中加载的配置类。
    7. 虽然我们127个场景的所有自动配置启动的时候默认全部加载,但是xxxxAutoConfiguration按照条件装配规则(@Conditional),最终会按需配置,整体装配的流程如下:
      • SpringBoot先加载所有的自动配置类xxxxxAutoConfiguration
      • 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值,生效的配置类就会给容器中装配很多组件,只要容器中有这些组件,相当于这些功能就有了。
        1. 自动配置类是否生效主要看是否符合@Conditional注解条件。
        2. 同时还会利用@EnableConfigurationProperties@ConfigurationProperties为容器中注册一个包含指定配置的类,该类的属性值与application.properties从文件中取。
        3. 在该类下的组件就可以利用该配置类进行初始化(初始化方法中的参数会自动在容器中寻找符合条件的)并注册到容器中,此时也就具备了对应组件的功能。
      • SpringBoot默认会在底层配置好所有组件,但是如果用户自己进行了配置,那么会以用户配置的优先。
        1. 组件:用户直接自己@Bean替换底层的组件吗,如果用户自定义了组件,SpringBoot根据@Conditional注解就会选择用户自定义的组件。
        2. 配置:用户去看这个组件是获取的配置文件什么值就去修改。
      • 注解:xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ---->application.properties。
  • 相关阅读:
    基于cobra的go语言命令行解析器
    QT 音乐播放器【二】 歌词同步+滚动+特效
    go服务如何使用静态文件?
    安装k8s集群
    使用Linux下的“共享内存“实现一些简单功能
    驱动——按键中断控制LED灯实验
    CSS选择器 前端开发入门笔记(十)
    1019 数字黑洞
    java调用天地图api获取地理信息
    算法小考试(有点难)
  • 原文地址:https://blog.csdn.net/weixin_44446626/article/details/126487192