SpringBoot demo项目初探

本文是接上文IDEA环境下springboot快速入门项目的分析


1.pom文件

1.1父项目

查看pom文件有如下代码:

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

可以看到它的父项目为spring-boot-starter-parent,点进去查看源码,可见如下代码:

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

点击查看其父项目\color{#008000}{spring-boot-dependencies源码,可见其对于依赖及依赖版本的管理},摘录部分代码如下:

<properties>
        <activemq.version>5.15.8</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine-sdk.version>1.9.68</appengine-sdk.version>
        <artemis.version>2.6.3</artemis.version>
        <aspectj.version>1.9.2</aspectj.version>
        <assertj.version>3.11.1</assertj.version>
        <!-- ...-->
</properties>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
        </dependencies>
        <!-- ...-->
</dependencyManagement>
<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.johnzon</groupId>
                    <artifactId>johnzon-maven-plugin</artifactId>
                    <version>${johnzon.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <version>${kotlin.version}</version>
                </plugin>
                <!-- ...-->
           </plugins>
      </pluginManagement>
</build>

1.2 pom文件导入的依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>
  • spring-boot-starter-web
    • spring-boot-starter:spring-boot场景启动器,帮我们导入了web模块正常运行所依赖的组件
    • web:代表web模块,如果需要其他模块,也就导入相应的spring-boot-starter-xxx模块即可
  • spring-boot-starter-test:同上,test代表test模块

SpringBoot将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目里面引入这些,starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。


2.项目源码分析

2.1主程序类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication用来标注一个主程序类,说明这是一个SpringBoot应用
 */
@SpringBootApplication
public class DemoApplication {

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

}

@SpringBootApplication:标注在某个类上,说明该应用是一个SpringBoot应用,这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类main方法来启动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 {
}

可见@SpringBootApplication是一个组合注解,其中:

  • @SpringBootConfiguration:SpringBoot的配置类
  • @EnableAutoConfiguration:开启自动配置功能,以前我们需要配置的东西,SpringBoot帮我们自动配置。
    点击查看@SpringBootConfiguration源码,部分如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}
  • @Configuration:配置类注解
    回到@SpringBootApplication源码
    点击查看@EnableAutoConfiguration源码,部分如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
  • @AutoConfigurationPackage:自动配置包
  • @Import(AutoConfigurationImportSelector.class):AutoConfigurationImportSelector为导入哪些组件的选择器
    点击查看@AutoConfigurationPackage源码,部分如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

}
  • @Import(AutoConfigurationPackages.Registrar.class):Spring底层注解,给容器中导入一个组件,导入的组件由AutoConfigurationPackages.Registrar指定。
    点击查看AutoConfigurationPackages.Registrar源码,如下:
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {

        @Override
        public void registerBeanDefinitions(AnnotationMetadata metadata,
                BeanDefinitionRegistry registry) {
            register(registry, new PackageImport(metadata).getPackageName());
        }

        @Override
        public Set<Object> determineImports(AnnotationMetadata metadata) {
            return Collections.singleton(new PackageImport(metadata));
        }

}

断点调试查看可知,参数metadata为注解元信息,计算new PackageImport(metadata).getPackageName()表达式的结果为:com.example.demo

可见:这里将主配置类(@SpringBootApplication标注的类)所在包及其子包下的所有组件扫描到Spring容器中。

回到@EnableAutoConfiguration源码
点击查看AutoConfigurationImportSelector类的源码,有如下方法:

public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                .loadMetadata(this.beanClassLoader);
        AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
                autoConfigurationMetadata, annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }

这里将需要导入的组件的全类名以String数组的方式返回,这些组件就会被添加到容器中,最终会给容器中导入非常多的AutoConfigurationMetadata(自动配置元数据):就是给容器中导入这个场景需要的所有组件,并配置好这些组件。有了自动配置类,就免去了我们手动编写配置注入功能组件等的工作。

打开External Libraries里如下路径:
图片.png

可以看到AutoConfigurationMetadata导入的自动配置类

可知,SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取AutoConfigurationMetadata指定的值,将这些值作为自动配置类导入到容器中。

关于SpringBootApplication总结:

  • @SpringBootApplication:说明该应用是一个SpringBoot应用
    • @SpringBootConfiguration:SpringBoot应用基本配置
      • @Configuration:配置类注解
    • @EnableAutoConfiguration:
      • @AutoConfigurationPackage:
        • @Import(AutoConfigurationPackages.Registrar.class):将主配置类(@SpringBootApplication标注的类)所在包及其子包下的所有组件扫描到Spring容器中。
      • @Import(AutoConfigurationImportSelector.class):自动给容器中导入场景所需要的所有组件,并配置好这些组件。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容