spring-boot 從0開始 第一章:helloworld與原理初探

1. SpringBoot 簡介

1.1 Spring - Rod Johnson

  • Spring是一個開源框架,2003 年興起的一個輕量級的Java 開發框架,作者:Rod Johnson
  • Spring是爲了解決企業級應用開發的複雜性而創建的,簡化開發

1.2 Spring 是如何簡化 Java 開發的

爲了降低Java開發的複雜性,Spring採用了以下4種關鍵策略:

  1. 基於POJO的輕量級和最小侵入性編程,所有東西都是bean
  2. 通過IOC,依賴注入(DI)和麪向接口實現鬆耦合
  3. 基於切面(AOP)和慣例進行聲明式編程
  4. 通過切面和模版減少樣式代碼,RedisTemplate,xxxTemplate

2. 第一個SpringBoot 程序

2.1 方式一:在 springboot 官方創建項目

  • 官方創建項目URL:https://start.spring.io/
  • 由於這個並不是官方推薦的創建項目方式,這裏並不多做介紹

2.2 方式二:在 IDEA 中創建項目

這裏使用的IDEA版本爲2019.3

setp 1

setp 2

setp 3

setp 4

setp 5

setp 6

2.3 更改項目的端口號

3. 原理初探

springboot 核心思想:自動裝配(配置)
maven 核心文件 pom.xml

3.1 父依賴

pom.xml

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

ctrl+點擊 <artifactId>spring-boot-starter-parent</artifactId>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

操作過程

  • spring-boot-dependencies:核心依賴 - 在父工程中

(如果操作沒有,嘗試更換 <version>2.2.0.RELEASE</version> 的版本)

  • 我們在寫或者引入一些springboot依賴的時候,不需要指定版本,就因爲這些版本倉庫

3.2 啓動器 ☆

啓動器 spring-boot-starter:說白了就是springboot的啓動場景

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>
  • spring-boot-starter-web 幫我們導入了web模塊正常運行的依賴組件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 比如spring-boot-starter-web,他就會幫我們自動導入web環境所有的依賴
  • springboot 會將所有的功能場景,都變成一個個的啓動器
  • 我們要使用什麼功能,就只需要找到對應的啓動器就可以了

3.3 啓動類 ☆

//@SpringBootApplication 來標註一個主程序類
//標註這個類是一個spring boot 應用
@SpringBootApplication
public class Springboot01HelloworldApplication {

    public static void main(String[] args) {
        // 將 springboot 應用啓動
        SpringApplication.run(Springboot01HelloworldApplication.class, args);
    }
}

run() 方法啓動了一個tomcat(默認)服務

3.3.1 分析啓動類註解

@SpringBootApplication

作用:標註在某個類上說明這個類是SpringBoot的主配置類 , SpringBoot就應該運行這個類的main方法來啓動SpringBoot應用;
ctrl+鼠標 點擊進入這個註解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.autoconfigure;

import ...

@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}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};

    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

@SpringBootConfiguration - springboot的配置

springboot的配置
|----@SpringBootApplication
|---- |---- @SpringBootConfiguration
|---- |----|---- @Configuration
|---- |----|---- |---- @Component 本質是組件

@EnableAutoConfiguration 自動配置

springboot的配置
|----@SpringBootApplication
|---- |---- @EnableAutoConfiguration 自動配置
|---- |----|---- @AutoConfigurationPackage 自動配置包
|---- |----|---- |---- @Import({Registrar.class}) 自動配置-包註冊
|---- |----|---- @Import({AutoConfigurationImportSelector.class}) 自動配置導入選擇

進入 AutoConfigurationImportSelector 這個類

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

獲取候選的配置

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

META-INF/spring.factories :自動配置的核心文件

所有的資源加載到配置類中

Properties properties = PropertiesLoaderUtils.loadProperties(resource);

結論:springboot所有自動配置都是在啓動的時候掃描並加載:spring.factories 所有的自動配置類都在這裏面,但是不一定生效,要判斷條件是否成立 @ConditionalOnXXX(),只要導入了對應的start,就有對應的啓動器了,有了啓動器,自動裝配就會生效,然後配置成功

3.4 檢測依賴 debug: true

application.yml 中 使用 debug: true

  • Positive matches :配置類啓動並生效的
  • Negative matches :沒有匹配的配置類

3.4 自動裝配的過程

  1. springboot在啓動的時候,從類路徑下 META-INF/spring.factories 獲取指定的值
  2. 將這些自動配置的類導入容器,自動配置就會生效,幫我進行自動配置
  3. 以前我們需要自動配置的東西,現在springboot幫我們做了
  4. 整合javaEE,解決方案和自動配置的東西都在 spring-boot-autoconfigure-2.2.0.RELEASE.jar 這個包下
  5. 他會把所有需要導入的組件,以類名的方式返回,這些組件就會被添加到容器
  6. 容器中也會存在非常多的xxxAutoConfiguration的文件(@Bean),就是這些類給容器中導入了這個場景需要的所有組件,並自動配置 @configuration ,javaconfig
  7. 有了自動配置類,免去了我們動手編寫配置文件的工作

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章