SpringBoot教程(万字图文详解)

SpringBoot教程(万字图文详解)-mikechen

SpringBoot简介

SpringBoot教程(万字图文详解)-mikechen

SpringBoot来简化Spring应用开发,约定大于配置,去繁化简,是 Spring开源组织下的子项目框架。

SpringBoot能够简化配置文件,快速构建web应用,内置tomcat,无需打包部署,直接运行。

 

为什么要使用SpringBoot

传统的 SSM/SSH 框架组合配置繁琐臃肿,不同项目有很多重复、模板化的配置,严重降低了 Java 工程师的开发效率。

而 Spring Boot 对 Spring 家族和一些第三方库提供一系列自动化配置的 Starter,来使得开发快速搭建一个基于 Spring 的应用程序。

 

SpringBoot快速构建

多说无益,实践为上,接下来,我就通过 Idea来建立起我们的第一个Spring Boot项目。

第一步:创建项目

首先在左上角的file->new->project,如下图所示:

SpringBoot教程(万字图文详解)-mikechen

第二步:SpringBoot初始化

选择左侧的Spring Initializer,如下图所示:

SpringBoot教程(万字图文详解)-mikechen

第三步:添加项目名称信息

选择JDK安装版本,以及填写项目名称和本地存储路径,如下图所示:

SpringBoot教程(万字图文详解)-mikechen

第四步:选择Maven依赖

选择依赖Spring Web,其他依赖看需要勾选,如下图所示:

SpringBoot教程(万字图文详解)-mikechen

然后点击Next,最后Finish项目创建完成,Springboot目录结构如下图所示:

SpringBoot教程(万字图文详解)-mikechen

第五步:编写Controller直接测试

SpringBoot教程(万字图文详解)-mikechen

@RestController
class HelloWordController {
    @GetMapping("/hello")
    public String helloWord() {
        return "Hello Word!";
    }
}

 

第六步:启动项目

找到启动类,鼠标右击运行springBoot项目,如下图所示:

SpringBoot教程(万字图文详解)-mikechen

访问地址:http://localhost:8080/hello

SpringBoot教程(万字图文详解)-mikechen

出现上图画图说明测试成功了。

 

SpringBoot启动原理

SpringBoot原理,我们开发任何一个Spring Boot项目,都会用到如下的启动类:

SpringBoot教程(万字图文详解)-mikechen

 

SpringBoot注解

我们点击这个注解,@SpringBootApplication,我标出来的这三个SpringBoot注解,是非常重要的。

如下图所示:

SpringBoot教程(万字图文详解)-mikechen

分别是:

  • @SpringBootConfiguration;
  • @ComponentScan;
  • @EnableAutoConfiguration;

虽然定义使用了多个Annotation进行了原信息标注,但实际上重要的只有三个Annotation。

自动配置@EnableAutoConfiguration

Spring Boot自动配置注解,开启这个注解之后,此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

 

组件扫描@ComponentScan

组件扫描其实很简单,@ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中。

比如:

@ComponentScan(“com.mikechen.test”)
@SpringBootApplication
public class SpringbootApplication {

这么做扫描的范围扩大到整个父包com.mikechen.test包。

@ComponentScan注解默认就会装配标识了:

  • @Controller
  • @Service
  • @Repository
  • @Component

 

SpringBoot配置文件

Spring Boot 官方提供了两种常用的SpringBoot配置文件格式,分别是:

1.properties格式

application.properties

2.yml格式

application.yml

这两个文件本质是一样的,区别只是其中的语法略微不同,相比于properties来说,yml更加年轻,层级也是更加分明。

 

SpringBoot配置文件加载顺序

SpringBoot项目启动会扫描以下位置的application.properties或者application.yml文件作为SpringBoot的默认配置文件。

比如:下面我以application.properties配置文件为例。

application.properties可以放置四个位置,优先级从1到4依次递减。

file:./config/ ( 项目根路径下的config文件夹)
file:./  (项目根路径)
classpath:/config/ (类路径下的config文件夹)
classpath:/ (类路径)

如下图所示:
SpringBoot教程(万字图文详解)-mikechen
优先级由高到底,高优先级的配置会覆盖低优先级的配置。

 

SpringBoot运行流程

SpringBoot运行流程,大致分为如下11大执行流程步骤:

SpringBoot教程(万字图文详解)-mikechen

 

1)创建 Spring Application 实例,调用 run 方法,同时将启动入口类作 为参数传递进去;

2)通过 Spring Factories Loader 加载 META-INF/spring.factories 文 件;

3)然后由 SpringApplicationRunListener 来发出 starting 消息;

4)创建参数,并配置当前 SpringBoot 应用需要使用的 Environment 实 例;

5)完成之后,依然由 SpringApplicationRunListener 来发出 environmentPrepared 消息;

6)创建 Spring 的应用上下文实例:ApplicationContext,初始化该实例 并设置应用环境配置实例:Environment,同时加载相关的配置项;

7)由 SpringApplicationRunListener 发出 contextPrepared 消息,告 知 SpringBoot 应用当前使用的 ApplicationContext 已准备完毕;

8)将各种 Bean 组件装载入 Spring 的 IO 容器/应用上下文: ApplicationContext 中,继续由 SpringApplicationRunListener 来发出 contextLoaded 消息,告知 SpringBoot 应用当前使用的 ApplicationContext 已准备完毕;

9)重新刷新 Refresh Spring 的应用上下文实例:ApplicationContext, 完成 IOC 容器可用的最后一步;

10)由 SpringApplicationRunListener 发出 started 消息,完成最终的 程序的启动;

11)由 SpringApplicationRunListener 发出 running 消息,告知程序已 成功运行起来了。

 

作者简介

陈睿|mikechen,10年+大厂架构经验,BAT资深面试官,就职于阿里巴巴、淘宝、百度等一线互联网大厂。

👇阅读更多mikechen架构文章👇

阿里架构 |双11秒杀 |分布式架构 |负载均衡 |单点登录 |微服务 |云原生 |高并发 |架构师

以上

关注作者「mikechen」公众号,获取更多技术干货!

后台回复架构,即可获取《阿里架构师进阶专题全部合集》,后台回复面试即可获取《史上最全阿里Java面试题总结

评论交流
    说说你的看法