SpringBoot2.x 啟動過程詳解


spring 簡化了java應用開發, 而springboot則簡化了 spring應用的開發,用約定優於配置優於編碼的方式快速構建spring對其他框架的整合.

官方文檔

探究Hello,World

使用spring 快速構建一個web應用:

新建一個maven項目

pom依賴:

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

編寫主啟動類:

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoApplication.class, args);
    }
}

controller 類:

@RestController
@RequestMapping
public class DemoController {
    @GetMapping("test")
    public String getString (){
        return "Hello World";
    }
}

運行主啟動類的main方法:

啟動成功,訪問http://127.0.0.1:8080/test 返回Hello World

就此一個簡單web應用就搭建完畢,

why?

  1. 我沒有引入 web相關的任何依賴呀?
  2. 就算導入了相關依賴 我也沒有配置任何信息,各種組件如何工作?
  3. 我也沒有用到容器,應用跑在哪里?
  4. 我編寫的controller為什么可以生效?

抱着這些疑問 探究一下

依賴:

spring-boot-starter-web是我們導入的唯一的一個依賴, 點進去一看就明白了,

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.14.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

他已經幫我們導好了web開發所常用的依賴, 這就是場景啟動器,Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在項目里面引入這些starter 相關場景的所有依賴都會導入進來。要用什么功能就導入什么場景的啟動器,

在看看 父工程:

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

他的作用就是版本仲裁,當我們導入場景啟動器 或者當自己導入依賴時 只要是在 springboot父工程中聲明過的 都無須自己定義版本,大大減少版本沖突的可能

<!--常用依賴的版本-->
<properties>
    
		<!--*******-->
    
        <commons-pool.version>1.6</commons-pool.version>
        <commons-pool2.version>2.6.0</commons-pool2.version>
        <couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
        <couchbase-client.version>2.7.2</couchbase-client.version>
        <derby.version>10.14.2.0</derby.version>
        <dom4j.version>1.6.1</dom4j.version>
        <dropwizard-metrics.version>4.0.5</dropwizard-metrics.version>
        <ehcache.version>2.10.6</ehcache.version>
        <ehcache3.version>3.6.3</ehcache3.version>
        <elasticsearch.version>6.4.3</elasticsearch.version>
        <embedded-mongo.version>2.1.2</embedded-mongo.version>
        <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
        <flatten-maven-plugin.version>1.0.1</flatten-maven-plugin.version>
        <flyway.version>5.2.4</flyway.version>
        <freemarker.version>2.3.28</freemarker.version>
        <git-commit-id-plugin.version>2.2.6</git-commit-id-plugin.version>
        <glassfish-el.version>3.0.0</glassfish-el.version>
        <glassfish-jaxb.version>2.3.1</glassfish-jaxb.version>
        <groovy.version>2.5.5</groovy.version>
        <gson.version>2.8.5</gson.version>
        <h2.version>1.4.197</h2.version>
        <hamcrest.version>1.3</hamcrest.version>
        <hazelcast.version>3.11.1</hazelcast.version>
        <hazelcast-hibernate5.version>1.2.3</hazelcast-hibernate5.version>
        <hibernate.version>5.3.7.Final</hibernate.version>
        <hibernate-validator.version>6.0.14.Final</hibernate-validator.version>
        <hikaricp.version>3.2.0</hikaricp.version>
        <hsqldb.version>2.4.1</hsqldb.version>
        <htmlunit.version>2.33</htmlunit.version>
        <httpasyncclient.version>4.1.4</httpasyncclient.version>
        <httpclient.version>4.5.6</httpclient.version>
        <httpcore.version>4.4.10</httpcore.version>
        <infinispan.version>9.4.5.Final</infinispan.version>
        <influxdb-java.version>2.14</influxdb-java.version>
        <jackson.version>2.9.8</jackson.version>
        <janino.version>3.0.11</janino.version>
        <javax-activation.version>1.2.0</javax-activation.version>
        <javax-annotation.version>1.3.2</javax-annotation.version>
        <javax-cache.version>1.1.0</javax-cache.version>
        <javax-jaxb.version>2.3.1</javax-jaxb.version>
        <javax-jaxws.version>2.3.1</javax-jaxws.version>
        <javax-jms.version>2.0.1</javax-jms.version>
        <javax-json.version>1.1.4</javax-json.version>
        <javax-jsonb.version>1.0</javax-jsonb.version>
        <javax-mail.version>1.6.2</javax-mail.version>
        <javax-money.version>1.0.3</javax-money.version>
        <javax-persistence.version>2.2</javax-persistence.version>
        <javax-transaction.version>1.3</javax-transaction.version>
        <javax-validation.version>2.0.1.Final</javax-validation.version>
        <javax-websocket.version>1.1</javax-websocket.version>
        <jaxen.version>1.1.6</jaxen.version>
        <jaybird.version>3.0.5</jaybird.version>
        <jboss-logging.version>3.3.2.Final</jboss-logging.version>
        <jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version>
        <jdom2.version>2.0.6</jdom2.version>
        <jedis.version>2.9.1</jedis.version>
        <jersey.version>2.27</jersey.version>
        <jest.version>6.3.1</jest.version>
        <jetty.version>9.4.14.v20181114</jetty.version>
        <jetty-el.version>8.5.35.1</jetty-el.version>
        <jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
        <jetty-reactive-httpclient.version>1.0.2</jetty-reactive-httpclient.version>
        <jmustache.version>1.14</jmustache.version>
        <jna.version>4.5.2</jna.version>
        <joda-time.version>2.10.1</joda-time.version>
        <johnzon.version>${johnzon-jsonb.version}</johnzon.version>
        <johnzon-jsonb.version>1.1.11</johnzon-jsonb.version>
        <jolokia.version>1.6.0</jolokia.version>
        <jooq.version>3.11.9</jooq.version>
        <jsonassert.version>1.5.0</jsonassert.version>
        <json-path.version>2.4.0</json-path.version>
        <jstl.version>1.2</jstl.version>
        <jtds.version>1.3.1</jtds.version>
        <junit.version>4.12</junit.version>
        <junit-jupiter.version>5.3.2</junit-jupiter.version>
        <kafka.version>2.0.1</kafka.version>
        <kotlin.version>1.2.71</kotlin.version>
        <lettuce.version>5.1.3.RELEASE</lettuce.version>
        <liquibase.version>3.6.2</liquibase.version>
        <log4j2.version>2.11.1</log4j2.version>
        <logback.version>1.2.3</logback.version>
        <lombok.version>1.18.4</lombok.version>
        <mariadb.version>2.3.0</mariadb.version>
        <maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
        <maven-assembly-plugin.version>3.1.1</maven-assembly-plugin.version>
        <maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
        <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
        <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
        <maven-enforcer-plugin.version>3.0.0-M2</maven-enforcer-plugin.version>
        <maven-failsafe-plugin.version>2.22.1</maven-failsafe-plugin.version>
        <maven-help-plugin.version>3.1.1</maven-help-plugin.version>
        <maven-install-plugin.version>2.5.2</maven-install-plugin.version>
        <maven-invoker-plugin.version>3.1.0</maven-invoker-plugin.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <maven-javadoc-plugin.version>3.0.1</maven-javadoc-plugin.version>
        <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
        <maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
        <maven-site-plugin.version>3.7.1</maven-site-plugin.version>
        <maven-source-plugin.version>3.0.1</maven-source-plugin.version>
        <maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
        <maven-war-plugin.version>3.2.2</maven-war-plugin.version>
        <micrometer.version>1.1.2</micrometer.version>
        <mimepull.version>1.9.10</mimepull.version>
        <mockito.version>2.23.4</mockito.version>
        <mongodb.version>3.8.2</mongodb.version>
        <mongo-driver-reactivestreams.version>1.9.2</mongo-driver-reactivestreams.version>
        <mssql-jdbc.version>6.4.0.jre8</mssql-jdbc.version>
        <mysql.version>8.0.13</mysql.version>
        <nekohtml.version>1.9.22</nekohtml.version>
        <neo4j-ogm.version>3.1.6</neo4j-ogm.version>
        <netty.version>4.1.31.Final</netty.version>
        <netty-tcnative.version>2.0.20.Final</netty-tcnative.version>
        <nio-multipart-parser.version>1.1.0</nio-multipart-parser.version>
        <pooled-jms-version>1.0.3</pooled-jms-version>
        <postgresql.version>42.2.5</postgresql.version>
        <prometheus-pushgateway.version>0.5.0</prometheus-pushgateway.version>
        <quartz.version>2.3.0</quartz.version>
        <querydsl.version>4.2.1</querydsl.version>
        <rabbit-amqp-client.version>5.4.3</rabbit-amqp-client.version>
        <reactive-streams.version>1.0.2</reactive-streams.version>
    
      <!--*******-->
    
    </properties>

 <dependencyManagement>
     
        <!--*******-->
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-couchbase-reactive</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jdbc</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-ldap</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-neo4j</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-solr</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-groovy-templates</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-hateoas</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-integration</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jersey</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jooq</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-json</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jta-atomikos</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jta-bitronix</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mustache</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-oauth2-client</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-reactor-netty</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-quartz</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-undertow</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>antlr</groupId>
                <artifactId>antlr</artifactId>
                <version>${antlr2.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-access</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>com.atomikos</groupId>
                <artifactId>transactions-jdbc</artifactId>
                <version>${atomikos.version}</version>
            </dependency>
            <dependency>
                <groupId>com.atomikos</groupId>
                <artifactId>transactions-jms</artifactId>
                <version>${atomikos.version}</version>
            </dependency>
            
          <!--*******-->
            
      </dependencies>
            </dependencyManagement>
       

這也就是我們不用導入web相關依賴 和 版本的原因

主啟動類:

main方法中調用SpringApplication 的 run方法,傳入了主啟動類對象, 主要是創建 ApplicationContext 上下文 並刷新容器,但身為容器中的一員,主啟動類上的注解也就生效了,SpringBootApplication

@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 {

這是該注解的源碼, 上面四個都是一些元注解,定義這個注解的一些屬性,例如作用域,是否生成文檔之類的 這里就不看了,
主要看 SpringBootConfiguration 和 EnableAutoConfiguration 的功能

@SpringBootConfiguration

這個注解比較簡單,下面是他的源碼,標注主啟動類也是一個config配置類,可以使用@Bean 向容器中注入組件

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration

例如如下代碼,容器中就自動注入了 自定義的RedisUtil類 ,可以在需要的地方注入使用:

@SpringBootApplication
public class DemoApplication {
    
    @Bean
    public static RedisUtil getRedisUtil() {
        return new RedisUtil();
    }
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@EnableAutoConfiguration

這個注解聽名字就很厲害,開啟自動配置, 這就是springboot 自動配置的核心,源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

AutoConfigurationPackage

通過 @Import(AutoConfigurationPackages.Registrar.class) Import注解 ,手動向容器中注入Bean

(@Import的具體用法 請參考 spring注解驅動),

調用Registrar 對象的 registerBeanDefinitions 方法 手動注入到容器中,而注入的對象 則會自動掃描啟動類所在的包 及其子包 中 標注 Service Controller Component 等 類:

這就是為什么 啟動類所在包及其子包下的類會自動注入的原因了

Import(AutoConfigurationImportSelector.class)

這個注解和上面的AutoConfigurationPackage 一樣 都是通過Import 注解進行注入Bean, 看看AutoConfigurationImportSelector 有什么玄機

主要就是 getAutoConfigurationEntry 方法中

protected AutoConfigurationEntry getAutoConfigurationEntry(
      AutoConfigurationMetadata autoConfigurationMetadata,
      AnnotationMetadata annotationMetadata) {
   if (!isEnabled(annotationMetadata)) {
      return EMPTY_ENTRY;
   }
   AnnotationAttributes attributes = getAttributes(annotationMetadata);
   /* 去 META-INF/spring.factories 中 尋找 			org.springframework.boot.autoconfigure.EnableAutoConfiguration  鍵所對應的值,並切割 逗號 獲得一個一個類名 ,這些類就是自動配置類*/
   List<String> configurations = getCandidateConfigurations(annotationMetadata,
         attributes);
   configurations = removeDuplicates(configurations);
   Set<String> exclusions = getExclusions(annotationMetadata, attributes);
    //根據用戶自定義的規則篩選
   checkExcludedClasses(configurations, exclusions);
   configurations.removeAll(exclusions);
    //過濾場景啟動器中沒有用到的
   configurations = filter(configurations, autoConfigurationMetadata);
   fireAutoConfigurationImportEvents(configurations, exclusions);
   return new AutoConfigurationEntry(configurations, exclusions);
}

獲取 文件中的配置,

得到的所有自動配置類,spring-boot-autoconfigure 包中的/META-INF/spring.factories中,這個是springboot官方集成的自動配置類

經過一系列的篩選 最后只返回了22 個自動配置類 ,而這些 則是spring-boot-starter-web 場景啟動器所需要的,

而這22個主啟動類, 則進行了所有默認的配置工作,滿足web 開發的基本需求

看看這個filter方法是怎么過濾掉不需要的自動配置類的:

private List<String> filter(List<String> configurations,
			AutoConfigurationMetadata autoConfigurationMetadata) {
    //記錄時間
		long startTime = System.nanoTime();
    //待過濾的 118個類
		String[] candidates = StringUtils.toStringArray(configurations);
    //是否過濾掉的標致 默認全部為false 代表全部不過濾
		boolean[] skip = new boolean[candidates.length];
    //是否有過濾掉自動配置類
		boolean skipped = false;
    /*一共有三個過濾器 : OnClassConditional , OnWebApplicationConditional,OnBeanConditional, 分別過濾自動配置類上的三種類型的注解,后面詳講*/
		for (AutoConfigurationImportFilter filter : getAutoConfigurationImportFilters()) {
            //加強過濾器
			invokeAwareMethods(filter);
            /*對 待過濾的118個類進行過濾,返回的boolean數組就是過濾結果對應下標為false代表不符合條件*/
			boolean[] match = filter.match(candidates, autoConfigurationMetadata);
            //根據過濾條件,對skip進行修改為true(過濾掉了),並刪除待過濾類列表的對應下標(置為null)
			for (int i = 0; i < match.length; i++) {
				if (!match[i]) {
					skip[i] = true;
					candidates[i] = null;
					skipped = true;
				}
			}
		}
    //如果 一個沒有被過濾掉 就直接返回
		if (!skipped) {
			return configurations;
		}
    //新的數組 返回實際的類
		List<String> result = new ArrayList<>(candidates.length);
    //將 已經過濾過的類數組 符合條件的 set入list中返回
		for (int i = 0; i < candidates.length; i++) {
			if (!skip[i]) {
				result.add(candidates[i]);
			}
		}
		if (logger.isTraceEnabled()) {
			int numberFiltered = configurations.size() - result.size();
			logger.trace("Filtered " + numberFiltered + " auto configuration class in "
					+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)
					+ " ms");
		}
		return new ArrayList<>(result);
	}

經過這個方法的過濾,剩下的自動配置類就是本次項目中實際需要的依賴.

下面用兩個配置類看一下自動配置類是怎么工作的

自動配置類

HttpEncodingAutoConfiguration 根據上面的截圖可以看到 這個自動配置類是被注入進來的,那這個類為什么會被注入進來呢:

//該類為一個自動配置類, 其中標注@Bean的方法會向容器中注入組件
@Configuration

/* 將配置文件中對應的值和HttpEncodingProperties綁定起來;並把 HttpEncodingProperties加入到ioc容器中  這個類就是讀取我們配置application.yml 的配置類,*/
@EnableConfigurationProperties(HttpProperties.class)

/*Spring底層 @Conditional注解(看我的另一個博客),根據不同的條件,如果 滿足指定的條件,整個配置類里面的配置就會生效;    判斷當前應用是否是servlet-based web應用,如果是,當前配置類生效 */
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)

/*判斷當前項目有沒有這個類 CharacterEncodingFilter;SpringMVC中進行亂碼解決的過濾器*/
@ConditionalOnClass(CharacterEncodingFilter.class)

/* 判斷配置文件中是否存在某個配置  spring.http.encoding.enabled 是否為true 默認為true;如果手動關閉了這個選項則這個類的組件不會注入 */
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)

public class HttpEncodingAutoConfiguration 

而決定這個類是否被過濾的最重要的兩個注解及是 @ConditionalOnWebApplication 和 @ConditionalOnClass

是否為web項目 是否為包含某個類 我們這個HelloWorld項目都是滿足了 則沒有被過濾掉

再看看這個類HttpProperties 也很關鍵 在自動配置類上 指定了這個類為配置類 點進去看一下:

下面是刪減后的HttpProperties:

//這個注解即會讀取配置文件中 spring.http.* 的信息,並組裝這個類的屬性
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {

    private Map<Locale, Charset> mapping;
    
	private Charset charset = DEFAULT_CHARSET;
    
	private final Encoding encoding = new Encoding();
	

	public static class Encoding {

		private Boolean force;

		private Boolean forceRequest;

	
		private Boolean forceResponse;


	}

}

相對的則是配置文件中這些信息 和上面的屬性一一對應:

再來看一個自動配置類為什么沒有被注入進來:

比如 RedisAutoConfiguration,看到源碼, 如果沒有導入相關依賴 則連idea都檢測出來了,所以@ConditionalOnClass(RedisOperations.class) 這個注解肯定是不滿足條件的

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

@Conditional 派生的注解

可以用這些注解去更加靈活的配置自動配置類

@Conditional擴展注解 : 作用(判斷是否滿足當前指定條件)
@ConditionalOnJava 系統的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 滿足SpEL表達式指定
@ConditionalOnClass 系統中有指定的類
@ConditionalOnMissingClass 系統中沒有指定的類
@ConditionalOnSingleCandidate 容器中只有一個指定的Bean,或者這個Bean是首選Bean
@ConditionalOnProperty 系統中指定的屬性是否有指定的值
@ConditionalOnResource 類路徑下是否存在指定資源文件
@ConditionalOnWebApplication 當前是web環境
@ConditionalOnNotWebApplication 當前不是web環境
@ConditionalOnJnd i JNDI存在指定項

嵌入式容器

Spring Boot包括對嵌入式Tomcat,Jetty , Undertow 和 Reactor-Netty(2.x中加入,為了兼容webflux) 服務器的支持。默認情況下,Springboot使用的是Tomcat作為嵌入式服務器,在監聽默認的端口8080。這是我們為什么不用自己配置Servlet容器的原因

查看pom文件的依賴 可以看到 spring-boot-starter-web 默認引入的就是 spring-boot-starter-tomcat,同樣我們也可以將容器切換為其他的 比如jetty

更改pom: 將默認的tomcat依賴排除 引入 jetty依賴 ,

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <groupId>org.springframework.boot</groupId>
        </dependency>
    </dependencies>

然后啟動即可, springboot已經根據環境自動切換 ,啟動成功

Jetty started on port(s) 8080 (http/1.1) with context path '/'

那么 springboot 是怎么進行對容器的自動配置的呢:

主要是這個類EmbeddedWebServerFactoryCustomizerAutoConfiguration ,這個類在/META/spring.factories 文件中被注入容器中,而下面的四個方法則 分別判斷當前環境中導入的容器 並注入對應的WebServerFactoryCustomizer 工廠初始化類

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {

    //Tomcat環境
	@Configuration
	@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
	public static class TomcatWebServerFactoryCustomizerConfiguration {

		@Bean
		public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
				Environment environment, ServerProperties serverProperties) {
			return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
		}

	}

	//Jetty環境
	@Configuration
	@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
	public static class JettyWebServerFactoryCustomizerConfiguration {

		@Bean
		public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
				Environment environment, ServerProperties serverProperties) {
			return new JettyWebServerFactoryCustomizer(environment, serverProperties);
		}

	}

	//Undertow 環境
	@Configuration
	@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
	public static class UndertowWebServerFactoryCustomizerConfiguration {

		@Bean
		public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
				Environment environment, ServerProperties serverProperties) {
			return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
		}

	}

    // Netty 環境 (需要webflux的支持 這里不做講解)
	@Configuration
	@ConditionalOnClass(HttpServer.class)
	public static class NettyWebServerFactoryCustomizerConfiguration {

		@Bean
		public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(
				Environment environment, ServerProperties serverProperties) {
			return new NettyWebServerFactoryCustomizer(environment, serverProperties);
		}
	}

}

我們以TomcatWebServerFactoryCustomizer舉例 ,這個類 在 customize 方法中 讀取 我們配置的各種配置,(例如端口), 並set入 ConfigurableTomcatWebServerFactory Tomcat 真正的工廠類中.

public class TomcatWebServerFactoryCustomizer implements
      WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory>, Ordered {

   @Override
   public void customize(ConfigurableTomcatWebServerFactory factory) {
      ServerProperties properties = this.serverProperties;
      ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
      PropertyMapper propertyMapper = PropertyMapper.get();
      propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
            .to(factory::setBaseDirectory);
      propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull()
            .as(Duration::getSeconds).as(Long::intValue)
            .to(factory::setBackgroundProcessorDelay);
      customizeRemoteIpValve(factory);
      propertyMapper.from(tomcatProperties::getMaxThreads).when(this::isPositive)
            .to((maxThreads) -> customizeMaxThreads(factory,
                  tomcatProperties.getMaxThreads()));
      propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
            .to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
      propertyMapper.from(this::determineMaxHttpHeaderSize).whenNonNull()
            .asInt(DataSize::toBytes).when(this::isPositive)
            .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
                  maxHttpHeaderSize));
      propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()
            .asInt(DataSize::toBytes)
            .to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
      propertyMapper.from(tomcatProperties::getMaxHttpPostSize).asInt(DataSize::toBytes)
            .when((maxHttpPostSize) -> maxHttpPostSize != 0)
            .to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
                  maxHttpPostSize));
      propertyMapper.from(tomcatProperties::getAccesslog)
            .when(ServerProperties.Tomcat.Accesslog::isEnabled)
            .to((enabled) -> customizeAccessLog(factory));
      propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull()
            .to(factory::setUriEncoding);
      propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
            .to((connectionTimeout) -> customizeConnectionTimeout(factory,
                  connectionTimeout));
      propertyMapper.from(tomcatProperties::getMaxConnections).when(this::isPositive)
            .to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
      propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
            .to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
      customizeStaticResources(factory);
      customizeErrorReportValve(properties.getError(), factory);
   }

ConfigurableTomcatWebServerFactory 工廠類中 手動構建一個 Tomcat對象 並進行相應的配置,至此 一個嵌入式容器就創建完成

@Override
	public WebServer getWebServer(ServletContextInitializer... initializers) {
		Tomcat tomcat = new Tomcat();
		File baseDir = (this.baseDirectory != null) ? this.baseDirectory
				: createTempDir("tomcat");
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		Connector connector = new Connector(this.protocol);
		tomcat.getService().addConnector(connector);
		customizeConnector(connector);
		tomcat.setConnector(connector);
		tomcat.getHost().setAutoDeploy(false);
		configureEngine(tomcat.getEngine());
		for (Connector additionalConnector : this.additionalTomcatConnectors) {
			tomcat.getService().addConnector(additionalConnector);
		}
		prepareContext(tomcat.getHost(), initializers);
		return getTomcatWebServer(tomcat);
	}


免責聲明!

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



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