1. 程式人生 > >SpringBoot 三 啟動原理

SpringBoot 三 啟動原理

啟動類

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

重要的只有三個Annotation:

  • @Configuration(@SpringBootConfiguration點開檢視發現裡面還是應用了@Configuration)
  • @EnableAutoConfiguration
  • @ComponentScan

使用如下的SpringBoot啟動類,整個SpringBoot應用依然可以與之前的啟動類功能對等:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication相當於集合了這三個

[email protected]

它就是JavaConfig形式的Spring Ioc容器的配置類使用的那個@Configuration,SpringBoot社群推薦使用基於JavaConfig的配置形式,所以,這裡的啟動類標註了@Configuration之後,本身其實也是一個IoC容器的配置類。
舉幾個簡單例子回顧下,XML跟config配置方式的區別:

  • 表達形式層面
    基於XML配置的方式是這樣:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       default-lazy-init="true">
    <!--bean定義-->
</beans>

基於JavaConfig的配置方式是這樣:

@Configuration
public class MockConfiguration{
    //bean定義
}

任何一個標註了@Configuration的Java類定義都是一個JavaConfig配置類。(自己編寫配置類的時候要加上@Configuration)

  • 註冊bean定義層面
    基於XML的配置形式是這樣:
<bean id="mockService" class="..MockServiceImpl">
    ...
</bean>

而基於JavaConfig的配置形式是這樣的:

@Configuration
public class MockConfiguration{
    @Bean
    public MockService mockService(){
        return new MockServiceImpl();
    }
}

任何一個標註了@Bean的方法,其返回值將作為一個bean定義註冊到Spring的IoC容器,方法名將預設成該bean定義的id。

  • 表達依賴注入關係層面
    為了表達bean與bean之間的依賴關係,在XML形式中一般是這樣:
<bean id="mockService" class="..MockServiceImpl">
    <propery name ="dependencyService" ref="dependencyService" />
</bean>

<bean id="dependencyService" class="DependencyServiceImpl"></bean>

基於JavaConfig的配置形式是這樣的:

@Configuration
public class MockConfiguration{
    @Bean
    public MockService mockService(){
        return new MockServiceImpl(dependencyService);
    }

    @Bean
    public DependencyService dependencyService(){
        return new DependencyServiceIml();
}

如果一個bean的定義依賴其他bean,則直接呼叫對應的JavaConfig類中依賴bean的建立方法就可以了。

[email protected]

@ComponentScan的功能其實就是自動掃描並載入符合條件的元件(比如@Component和@Repository等)或者bean定義,最終將這些bean定義載入到IoC容器中。

我們可以通過basePackages等屬性來細粒度的定製@ComponentScan自動掃描的範圍,如果不指定,則預設Spring框架實現會從宣告@ComponentScan所在類的package進行掃描。

注:所以SpringBoot的啟動類最好是放在root package下,因為預設不指定basePackages。

[email protected] 

還記得Spring框架提供的各種名字為@Enable開頭的Annotation定義,如@EnableScheduling、@EnableCaching、@EnableMBeanExport等,@EnableAutoConfiguration的理念和做事方式其實一脈相承,簡單概括一下就是,

藉助@Import的支援,收集和註冊特定場景相關的bean定義

其中,最關鍵的要屬@Import(EnableAutoConfigurationImportSelector.class),藉助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以幫助SpringBoot應用將所有符合條件的@Configuration配置都載入到當前SpringBoot建立並使用的IoC容器。就像一隻“八爪魚”一樣

藉助於Spring框架原有的一個工具類:SpringFactoriesLoader的支援,@EnableAutoConfiguration可以智慧的自動配置功效才得以大功告成!

                                              

 自動配置幕後英雄:SpringFactoriesLoader類詳解
SpringFactoriesLoader屬於Spring框架私有的一種擴充套件方案,其主要功能就是從指定的配置檔案META-INF/spring.factories載入配置。

public abstract class SpringFactoriesLoader {
    //...
    public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
        ...
    }


    public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
        ....
    }
}

配合@EnableAutoConfiguration使用的話,它更多是提供一種配置查詢的功能支援,即根據@EnableAutoConfiguration的完整類名org.springframework.boot.autoconfigure.EnableAutoConfiguration作為查詢的Key,獲取對應的一組@Configuration類

 所以,@EnableAutoConfiguration自動配置:從classpath中搜尋所有的META-INF/spring.factories配置檔案,並將其中org.springframework.boot.autoconfigure.EnableutoConfiguration對應的配置項通過反射(Java Refletion)例項化為對應的標註了@Configuration的JavaConfig形式的IoC容器配置類,然後彙總為一個並載入到IoC容器。

 二.探索SpringApplication執行流程