• SpringBoot-03-Springboot自动装配原理


    3.Springboot自动装配原理

    • 核心依赖在父工程中:spring-boot-starter-parent—>spring-boot-dependencies.xml

    • 资源导出设置:spring-boot-starter-parent.xml

    • 插件设置:spring-boot-starter-parent.xml,若下载失败需要重新maven/install下

    • springboot是通过main方法下的SpringApplication.run方法启动的,启动的时候他会调用refshContext方法,先刷新容器,然后根据解析注解或者解析配置文件的形式注册bean,而它是通过启动类的SpringBootApplication注解进行开始解析的,他会根据EnableAutoConfiguration开启自动化配置,里面有个核心方法ImportSelect选择性的导入,根据loadFanctoryNames根据classpash路径以MATA-INF/spring.factorces下面以xxxEnableAutoConfiguration开头的key去加载里面所有对应的自动化配置,他并不是把这一百二十多个自动化配置全部导入,在他每个自动化配置里面都有条件判断注解,先判断是否引入相互的jar包,再判断容器是否有bean再进行注入到bean容器

    3.1 Springboot starter启动器

    • 传统的 Spring 项目想要运行,不仅需要导入各种依赖,还要对各种 XML 配置文件进行配置,十分繁琐,但 Spring Boot 项目在创建完成后,即使不编写任何代码,不进行任何配置也能够直接运行,这都要归功于 Spring Boot 的 starter 机制

    • Spring Boot 将日常企业应用研发中的各种场景都抽取出来,做成一个个的 starter(启动器),starter中整合了该场景下各种可能用到的依赖,用户只需要在Maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter提供了大量的自动配置,让用户摆脱了处理各种依赖和配置的困扰。所有这些 starter都遵循着约定成俗的默认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。即Springboot的启动场景;

    • 在引入一些Springboot依赖的时候,不需要指定版本,就因为有上面dependencies这些版本仓库

    并不是所有的 starter 都是由 Spring Boot 官方提供的,也有部分 starter 是第三方技术厂商提供的,例如 druid-spring-boot-starter和mybatis-spring-boot-starter 等等。当然也存在个别第三方技术,Spring Boot 官方没提供 starter,第三方技术厂商也没有提供 starter。

    • 以 spring-boot-starter-web 为例,它能够为提供Web开发场景所需要的几乎所有依赖,因此在使用Spring Boot开发Web项目时,只需要引入该Starter即可,而不需要额外导入 Web 服务器和其他的 Web 依赖。
    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.7.0
             
        
        com.example
        springbootdemo
        0.0.1-SNAPSHOT
        springbootdemo
        Demo project for Spring Boot
        
            12
        
        
            
            
                org.springframework.boot
                spring-boot-starter
            
            
            
                org.springframework.boot
                spring-boot-starter-mail
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
            
                org.springframework.boot
                spring-boot-starter-web
            
        
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 在该项目中执行以下 mvn 命令查看器依赖树。mvn dependency:tree
    [INFO] com.example:springbootdemo:jar:0.0.1-SNAPSHOT
    [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.7.0:compile
    [INFO] |  +- org.springframework.boot:spring-boot:jar:2.7.0:compile
    [INFO] |  |  \- org.springframework:spring-context:jar:5.3.20:compile
    [INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.0:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.0:compile
    [INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.2.11:compile
    [INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.2.11:compile
    [INFO] |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
    [INFO] |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
    [INFO] |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
    [INFO] |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
    [INFO] |  +- org.springframework:spring-core:jar:5.3.20:compile
    [INFO] |  |  \- org.springframework:spring-jcl:jar:5.3.20:compile
    [INFO] |  \- org.yaml:snakeyaml:jar:1.30:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:2.7.0:compile
    [INFO] |  +- org.springframework:spring-context-support:jar:5.3.20:compile
    [INFO] |  |  \- org.springframework:spring-beans:jar:5.3.20:compile
    [INFO] |  \- com.sun.mail:jakarta.mail:jar:1.6.7:compile
    [INFO] |     \- com.sun.activation:jakarta.activation:jar:1.2.2:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.0:test
    [INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.7.0:test
    [INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.0:test
    [INFO] |  +- com.jayway.jsonpath:json-path:jar:2.7.0:test
    [INFO] |  |  +- net.minidev:json-smart:jar:2.4.8:test
    [INFO] |  |  |  \- net.minidev:accessors-smart:jar:2.4.8:test
    [INFO] |  |  |     \- org.ow2.asm:asm:jar:9.1:test
    [INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.36:compile
    [INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
    [INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
    [INFO] |  +- org.assertj:assertj-core:jar:3.22.0:test
    [INFO] |  +- org.hamcrest:hamcrest:jar:2.2:test
    [INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test
    [INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
    [INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
    [INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
    [INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
    [INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
    [INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
    [INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
    [INFO] |  +- org.mockito:mockito-core:jar:4.5.1:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.12.10:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.12.10:test
    [INFO] |  |  \- org.objenesis:objenesis:jar:3.2:test
    [INFO] |  +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test
    [INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
    [INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
    [INFO] |  +- org.springframework:spring-test:jar:5.3.20:test
    [INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.0:test
    [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.7.0:compile
    [INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:2.7.0:compile
    [INFO]    |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:compile
    [INFO]    |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:compile
    [INFO]    |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:compile
    [INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.3:compile
    [INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.3:compile
    [INFO]    |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:compile
    [INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.0:compile
    [INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.63:compile
    [INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.63:compile
    [INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.63:compile
    [INFO]    +- org.springframework:spring-web:jar:5.3.20:compile
    [INFO]    \- org.springframework:spring-webmvc:jar:5.3.20:compile
    [INFO]       +- org.springframework:spring-aop:jar:5.3.20:compile
    [INFO]       \- org.springframework:spring-expression:jar:5.3.20:compile
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 我们可以看到 Spring Boot 导入了 springframework、logging、jackson 以及 Tomcat 等依赖,而这些正是我们在开发 Web 项目时所需要的。
    • 一个问题,即在以上 pom.xml 的配置中,引入依赖 spring-boot-starter-web 时,并没有指明其版本(version),但在依赖树中,我们却看到所有的依赖都具有版本信息,那么这些版本信息是在哪里控制的呢?

    这些版本信息是由 spring-boot-starter-parent(版本仲裁中心) 统一控制的。

    3.2 spring-boot-starter-web(Web启动器)

    • Spring MVC是Spring提供的一个基于MVC设计模式的轻量级Web开发框架,其本身就是Spring框架的一部分,可以与Spring 无缝集成,性能方面具有先天的优越性,是当今业界最主流的 Web 开发框架之一。

    • Spring Boot 是在Spring的基础上创建一款开源框架,它提供了spring-boot-starter-web(Web 场景启动器)来为 Web 开发予以支持。spring-boot-starter-web为我们提供了嵌入的Servlet容器以及SpringMVC的依赖,并为Spring MVC提供了大量自动配置,可以适用于大多数 Web 开发场景。

    3.2.1 Spring Boot Web 快速开发

    Spring Boot 为 Spring MVC 提供了自动配置,并在 Spring MVC默认功能的基础上添加了以下特性:

    • 引入了 ContentNegotiatingViewResolver 和 BeanNameViewResolver(视图解析器)
    • 对包括WebJars在内的静态资源的支持
    • 自动注册Converter、GenericConverter和Formatter (转换器和格式化器)
    • 对 HttpMessageConverters 的支持(Spring MVC中用于转换HTTP请求和响应的消息转换器)
    • 自动注册MessageCodesResolver(用于定义错误代码生成规则)
    • 支持对静态首页(index.html)的访问
    • 自动使用ConfigurableWebBindingInitializer

    只要我们在Spring Boot项目中的pom.xml中引入了 spring-boot-starter-web ,即使不进行任何配置,也可以直接使用Spring MVC 进行Web 开发。

    3.3 spring-boot-starter-parent

    • spring-boot-starter-parent是所有Spring Boot项目的父级依赖,它被称为Spring Boot的版本仲裁中心,可以对项目内的部分常用依赖进行统一管理。
    
            org.springframework.boot
            spring-boot-starter-parent
            2.7.0
             
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Spring Boot项目可以通过继承spring-boot-starter-parent来获得一些合理的默认配置,它主要提供了以下特性:

    • 默认JDK版本(Java 8)
    • 默认字符集(UTF-8)
    • 依赖管理功能
    • 资源过滤
    • 默认插件配置
    • 识别application.properties和application.yml类型的配置文件

    查看 spring-boot-starter- parent的底层代码,可以发现其有一个父级依赖 spring-boot-dependencies。

    
        org.springframework.boot
        spring-boot-dependencies
        2.7.0
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • spring-boot-dependencies的底层代码如下。
    
    
      4.0.0
      org.springframework.boot
      spring-boot-dependencies
      2.7.0
      pom
      spring-boot-dependencies
      Spring Boot Dependencies
      https://spring.io/projects/spring-boot
      
        
          Apache License, Version 2.0
          https://www.apache.org/licenses/LICENSE-2.0
        
      
      
        
          Pivotal
          info@pivotal.io
          Pivotal Software, Inc.
          https://www.spring.io
        
      
      
        https://github.com/spring-projects/spring-boot
      
      
        5.16.5
        2.7.7
        1.9.96
        2.19.1
        ...
        
        
        
          
            org.apache.activemq
            activemq-amqp
            ${activemq.version}
          
          
            org.apache.activemq
            activemq-blueprint
            ${activemq.version}
          
          
            org.apache.activemq
            activemq-broker
            ${activemq.version}
          
          ...
          
          
        
          
            
              org.codehaus.mojo
              build-helper-maven-plugin
              ${build-helper-maven-plugin.version}
            
            
              org.flywaydb
              flyway-maven-plugin
              ${flyway.version}
            
            ...
             
             
             
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    以上配置中,部分元素说明如下:

    • dependencyManagement :负责管理依赖;

    • pluginManagement:负责管理插件;

    • properties:负责定义依赖或插件的版本号。

    • spring-boot-dependencies 通过 dependencyManagement 、pluginManagement 和 properties等元素对一些常用技术框架的依赖或插件进行了统一版本管理,例如 Activemq、Spring、Tomcat 等。

    下一篇:SpringBoot-04-主启动程序分析

  • 相关阅读:
    redis学习-发布订阅
    网络安全与TikTok:年轻一代的数字素养
    第7章 函数--C++的编成模块
    vue项目中使用本地下载的iconfont文件中的图标,全部显示的都是方框
    服务器远程桌面经常连接不上,造成远程桌面连接不上的原因都有哪些
    gici-open示例数据运行(1.1开阔环境数据运行)
    《Upload-Labs》01. Pass 1~13
    Nginx 配置 https
    hevc 继续色度半像素差值
    特殊版:SpreadJS v15.1 VS SpreadJS v15.0
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126335468