springboot入門之版本依賴和自動配置原理


前言

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

Spring Boot 能快速創建出生產級別的Spring應用

  • Create stand-alone Spring applications

    • 創建獨立Spring應用
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    • 內嵌web服務器,無需再配置tomcat
  • Provide opinionated 'starter' dependencies to simplify your build configuration

    • starter場景啟動器自動配置相關依賴,簡化構建配置
  • Automatically configure Spring and 3rd party libraries whenever possible

    • 自動配置Spring以及第三方功能
  • Provide production-ready features such as metrics, health checks, and externalized configuration

    • 提供生產級別的監控、健康檢查及外部化配置
  • Absolutely no code generation and no requirement for XML configuration

    • 無代碼生成、無需編寫XML

boot:起始

SpringBoot是整合Spring技術棧的一站式框架,是簡化Spring技術棧的快速開發腳手架,在spring boot項目中可以很方便地使用Spring生態的服務,類似vue腳手架

依賴管理和版本仲裁

依賴管理

父項目一般做依賴管理

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>

<!-- 點進去后有依賴管理 -->
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>

版本仲裁

  • 引入默認依賴可以不寫版本號,子項目添加父項目存在的依賴可以不寫版本號
  • 引入非版本仲裁的jar,要寫版本號。

以swagger為例,父項目中規定依賴版本

<properties>
    <java.version>1.8</java.version>
    <swagger.version>2.7.0</swagger.version>
</properties>

<dependencyManagement>
    <dependencies>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!--swagger ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子項目可以不寫版本號

<dependencies>
    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
</dependencies>

修改默認版本號

  • 默認依賴也可以修改成自己想要的版本
<!--
	1、查看spring-boot-dependencies里面規定當前依賴的版本
	2、可以在當前項目里面重寫配置
-->
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>

自動配置

starter場景啟動器

在項目pom.xml文件中會看到很多boot-starter結尾的依賴名稱,只要引入starter,這個場景的所有常規依賴springboot都自動引入 ,例如mybatis-plus

<!--mybatis-plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${mybatis-plus.version}</version>
</dependency>

點開查看,發現使用mybatis-plus需要的依賴都在,不再需要自己手動添加,這就是springboot的便捷之處

<dependencies>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>3.0.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.20</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>optional</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.10.19</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

autoconfigure

每個場景啟動器都會有autoconfigure配置,這也是為什么springboot會自動配置依賴的

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.0.1.RELEASE</version>
      <scope>compile</scope>
</dependency>

默認包結構(重要)

  • 主程序所在包及其下面的所有子包里面的組件都會被默認掃描進來,這也是為什么啟動類必須直接放在項目模塊下,與controller層service層同級
  • 無需以前的包掃描配置
  • 想要改變掃描路徑,@SpringBootApplication(scanBasePackages="")
  • 或者@ComponentScan("")指定掃描路徑
@SpringBootApplication
//等同於
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan()

淺談springboot自動配置原理

配置的默認值

  • 各種配置都擁有默認值,例如tomcat端口默認8080
    默認配置最終都是映射到某個類上
  • 配置文件的值最終會綁定每個類上,這個類會在容器中創建對象

按需配置

按需加載所有自動配置項

  • 非常多的starter
  • 引入了場景之后,這個場景的自動配置才會開啟
  • SpringBoot所有的自動配置功能都在 spring-boot-autoconfigure 包里面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM