SpringBoot(一) 初識SpringBoot

來源:互聯網
上載者:User

標籤:構造   inf   slist   init   clu   nbsp   coder   註解   efi   

概述

首先我們看一下boot的啟動類,很明顯這裡面有兩個重點:

1,@SpringBootApplication

2, SpringApplication的初始化

 

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

          SpringApplication.run(DemoApplication.class, args);

}

}

 

我們分別來研究下這兩個重點,

哈哈,準備好了,我們準備開始了。

首先我們看一下@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 {

 

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

}

 

源碼瞭解一下:可以看出來@SpringBootApplication 被 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 註解所修飾,換言之 Springboot 提供了統一的註解來替代以上三個註解,簡化程式的配置。具體這些註解的作用,大家自行瞭解一下,這裡不再陳述。

 

其次,我們揭開SpringApplication的面紗。

SpringApplication 這個類應該算是 SpringBoot 架構 的“創新”產物了,原始的 Spring中並沒有這個類,SpringApplication 裡面封裝了一套 Spring 應用的啟動流程,然而這對使用者完全透明,因此我們上手 SpringBoot 時感覺簡潔、輕量。

SpringBoot 啟動類就是調用SpringApplication .run()方法來啟動的。

我們先看看SpringApplication 的構造方法,看看SpringApplication是如何建立的。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {

this.resourceLoader = resourceLoader;

Assert.notNull(primarySources, "PrimarySources must not be null");

this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));

this.webApplicationType = deduceWebApplicationType();

setInitializers((Collection) getSpringFactoriesInstances(

ApplicationContextInitializer.class));

setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

this.mainApplicationClass = deduceMainApplicationClass();

}

大概分為四個步驟:

1,deduceWebApplicationType():偵測應用的類型:建立的是 REACTIVE應用、SERVLET應用、NONE 三種中的某一種。

2,setInitializers:使用 SpringFactoriesLoader尋找並載入 classpath下 META-INF/spring.factories檔案中所有可用的 ApplicationContextInitializer。

3,setListeners:使用 SpringFactoriesLoader尋找並載入 classpath下 META-INF/spring.factories檔案中的所有可用的 ApplicationListener。

4,deduceMainApplicationClass:推斷並設定 main方法的定義類。

 

執行完上面的步驟,我們就完美的建立了SpringApplication類。

 

多寫代碼多讀書,做個有氣質的Coder。

SpringBoot(一) 初識SpringBoot

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.