三瘸恼、SpringBoot原理分析

三供汛、SpringBoot原理分析

3.1 起步依賴原理分析

3.1.1 分析spring-boot-starter-parent

按住Ctrl點(diǎn)擊pom.xml中的spring-boot-starter-parent纸镊,跳轉(zhuǎn)到了spring-boot-starter-parent的pom.xml被去,xml配置如下(只摘抄了部分重點(diǎn)配置):

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

按住Ctrl點(diǎn)擊pom.xml中的spring-boot-starter-dependencies梯皿,跳轉(zhuǎn)到了spring-boot-starter-dependencies的pom.xml仇箱,xml配置如下(只摘抄了部分重點(diǎn)配置):

<properties>
    <activemq.version>5.15.3</activemq.version>
    <antlr2.version>2.7.7</antlr2.version>
    <appengine-sdk.version>1.9.63</appengine-sdk.version>
    <artemis.version>2.4.0</artemis.version>
    <aspectj.version>1.8.13</aspectj.version>
    <assertj.version>3.9.1</assertj.version>
    <atomikos.version>4.0.6</atomikos.version>
    <bitronix.version>2.1.4</bitronix.version>
    <build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
    <byte-buddy.version>1.7.11</byte-buddy.version>
    ... ... ...
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        ... ... ...
    </dependencies>
</dependencyManagement>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>${jooq.version}</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.1.RELEASE</version>
            </plugin>
            ... ... ...
        </plugins>
    </pluginManagement>
</build>

從上面的spring-boot-starter-dependencies的pom.xml中我們可以發(fā)現(xiàn),一部分坐標(biāo)的版本东羹、依賴管理剂桥、插件管理已經(jīng)定義好,所以我們的SpringBoot工程繼承spring-boot-starter-parent后已經(jīng)具備版本鎖定等配置了属提。所以起步依賴的作用就是進(jìn)行依賴的傳遞权逗。

3.1.2 分析spring-boot-starter-web

按住Ctrl點(diǎn)擊pom.xml中的spring-boot-starter-web,跳轉(zhuǎn)到了spring-boot-starter-web的pom.xml冤议,xml配置如下(只摘抄了部分重點(diǎn)配置):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starters</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.1.RELEASE</version>
    <name>Spring Boot Web Starter</name>
  
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.9.Final</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

從上面的spring-boot-starter-web的pom.xml中我們可以發(fā)現(xiàn)斟薇,spring-boot-starter-web就是將web開(kāi)發(fā)要使用的spring-web、spring-webmvc等坐標(biāo)進(jìn)行了“打包”恕酸,這樣我們的工程只要引入spring-boot-starter-web起步依賴的坐標(biāo)就可以進(jìn)行web開(kāi)發(fā)了堪滨,同樣體現(xiàn)了依賴傳遞的作用。

3.2 自動(dòng)配置原理解析

按住Ctrl點(diǎn)擊查看啟動(dòng)類MySpringBootApplication上的注解@SpringBootApplication

@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);
    }
}

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

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    @AliasFor(annotation = EnableAutoConfiguration.class)
    Class<?>[] exclude() default {};

    ... ... ...

}

其中蕊温,

@SpringBootConfiguration:等同與@Configuration袱箱,既標(biāo)注該類是Spring的一個(gè)配置類

@EnableAutoConfiguration:SpringBoot自動(dòng)配置功能開(kāi)啟

按住Ctrl點(diǎn)擊查看注解@EnableAutoConfiguration

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

其中,@Import(AutoConfigurationImportSelector.class) 導(dǎo)入了AutoConfigurationImportSelector類

按住Ctrl點(diǎn)擊查看AutoConfigurationImportSelector源碼

public String[] selectImports(AnnotationMetadata annotationMetadata) {
        ... ... ...
        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 StringUtils.toStringArray(configurations);
}


protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
            AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
                getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
        
        return configurations;
}

其中寿弱,SpringFactoriesLoader.loadFactoryNames 方法的作用就是從META-INF/spring.factories文件中讀取指定類對(duì)應(yīng)的類名稱列表

spring.factories 文件中有關(guān)自動(dòng)配置的配置信息如下:

... ... ...

org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\

... ... ...

上面配置文件存在大量的以Configuration為結(jié)尾的類名稱犯眠,這些類就是存有自動(dòng)配置信息的類按灶,而SpringApplication在獲取這些類名后再加載

我們以ServletWebServerFactoryAutoConfiguration為例來(lái)分析源碼:

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
        ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
        ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
        ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
    ... ... ...
}

其中症革,

@EnableConfigurationProperties(ServerProperties.class) 代表加載ServerProperties服務(wù)器配置屬性類

進(jìn)入ServerProperties.class源碼如下:

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

    /**
     * Server HTTP port.
     */
    private Integer port;

    /**
     * Network address to which the server should bind.
     */
    private InetAddress address;
  
    ... ... ...
  
}

其中,

prefix = "server" 表示SpringBoot配置文件中的前綴鸯旁,SpringBoot會(huì)將配置文件中以server開(kāi)始的屬性映射到該類的字段中噪矛。映射關(guān)系如下:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市铺罢,隨后出現(xiàn)的幾起案子艇挨,更是在濱河造成了極大的恐慌,老刑警劉巖韭赘,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缩滨,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)脉漏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門苞冯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人侧巨,你說(shuō)我怎么就攤上這事舅锄。” “怎么了司忱?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵皇忿,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我坦仍,道長(zhǎng)鳍烁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任桨踪,我火速辦了婚禮老翘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘锻离。我一直安慰自己铺峭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布汽纠。 她就那樣靜靜地躺著卫键,像睡著了一般。 火紅的嫁衣襯著肌膚如雪虱朵。 梳的紋絲不亂的頭發(fā)上莉炉,一...
    開(kāi)封第一講書(shū)人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音碴犬,去河邊找鬼絮宁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛服协,可吹牛的內(nèi)容都是我干的绍昂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼偿荷,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼窘游!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起跳纳,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤忍饰,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后寺庄,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體艾蓝,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡力崇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了赢织。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片餐曹。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖敌厘,靈堂內(nèi)的尸體忽然破棺而出台猴,到底是詐尸還是另有隱情,我是刑警寧澤俱两,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布饱狂,位于F島的核電站,受9級(jí)特大地震影響宪彩,放射性物質(zhì)發(fā)生泄漏休讳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一尿孔、第九天 我趴在偏房一處隱蔽的房頂上張望俊柔。 院中可真熱鬧,春花似錦活合、人聲如沸雏婶。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)留晚。三九已至,卻和暖如春告嘲,著一層夾襖步出監(jiān)牢的瞬間错维,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工橄唬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赋焕,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓仰楚,卻偏偏與公主長(zhǎng)得像隆判,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子缸血,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容