zoukankan      html  css  js  c++  java
  • Springboot启动原理分析

    1.Springboot启动原理分析

    1.1 继承spring-boot-starter-parent,就相应的继承了一些版本控制的东西

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    

    点击spring-boot-starter-parent,发现它又继承spring-boot-dependencies

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.5.0</version>
      </parent>
    

    image

    点击spring-boot-dependencies,里面有maven 依赖的相应版本依赖引入
    image

    1.2 看一下spring-boot-starter-web帮我们做了什么事

    里面引入了springmvc,tomcat等等东西
    image

    点一下spring-boot-starter-json看一下
    image

    点一下看看spring-boot-starter-tomcat看一下,里面嵌套了tomcat的相关东西
    image

    2.Springboot的自动配置

    1.点击注解@SpringBootApplication
    image

    2.再点击注解@SpringBootConfiguration,结果发现其实就是一个配置类,也就是说SpringBootApplication本身就具备一个配置类configuration的功能
    image

    3.点击1出来的截图上的注解@EnableAutoConfiguration,这是springboot自动配置的核心注解

    4.componetScan组件扫描,约定了这个启动类所在的包,及其所有子包都进行扫描

    其实@SpringBootApplication注解是可以代替3个注解的作用
    Configuration,ComponetScan,EnableAutoConfiguration的作用

    3.Springboot的核心注解EnableAutoConfiguration

    是否可以自动配置
    image
    点击AutoConfigurationImportSelector
    image

    点击进去,找到最核心的getCandidateConfigurations
    image

    当前的类所在的jar包的META-INF/spring.fact
    org.springframework.boot.autoconfigure
    image

    image

    找到spring.factories
    image

    举个例子找servlet关键字里的ServletWebServerFactoryAutoConfiguration,发现里面有一个EnableConfigurationProperties(ServerProperties.class)
    image

    image

    发现里面都是配置
    具体默认的值在哪呢
    是在和META-INF/spring-configuration-metadata.json里

    {
          "name": "server.port",
          "type": "java.lang.Integer",
          "description": "Server HTTP port.",
          "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
          "defaultValue": 8080
        }
    

    4.autoconfigure

    覆盖默认配置
    application.properties

    server.port=8081
    server.servlet.context-path=/demo
    
    原创:做时间的朋友
  • 相关阅读:
    【剑指offer】数组中重复的数字
    【剑指offer】数组中只出现一次的数字
    【linux】进程存储管理
    【linux】gdb调试
    【C/C++】快速排序的两种实现思路
    【C/C++】知识点
    【计算机网络】知识点记录
    【hadoop】mapreduce原理总结
    基于社交网络的情绪化分析IV
    Android studio 升级,不用下载完整版,完美更新到2.0
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/15480870.html
Copyright © 2011-2022 走看看