1、Spring Boot简介

简化Spring应用开发的一个框架

整个Spring技术栈的一个大整合

J2EE开发的一站式解决方案

2、微服务

2014,Martin Fowler微服务论文

微服务:架构风格(服务微化)

每一个功能元素最终都是一个可独立替换和独立升级的软件单元

一个应用应该是一组小型服务:可以通过HTTP的方式进行互通

单体应用

环境约束(我的开发环境):

  • JDK8:java version "1.8.0_131"
  • Maven3.x:Apache Maven 3.6.3
  • IntelliJ IDEA2019.2
  • SpringBoot 1.5.9.RELEASE

3、Spring Boot HelloWorld

一个功能:

浏览器发送请求,服务器接收请求并处理,响应"Hello World"字符串

3.1 创建一个Maven工程

3.2 导入依赖Spring Boot相关的依赖

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

3.3 编写一个主程序

启动Spring Boot应用

@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}

3.4 编写相关的Controller、Service层

3.5 运行主程序测试

3.6 简化部署

Spring官方部署文档

<!-- 这个插件可以将应用打包成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

要运行该应用程序,请使用java -jar命令:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

4、Hello World探究

4.1 POM文件

父项目

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

Ctrl+鼠标左键查看底层源码,上面这个父项目还依赖于

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

实现真正管理Spring Boot应用里面的所有依赖版本

Spring Boot的版本仲裁中心

<properties>
<!-- Dependency versions -->
<activemq.version>5.14.5</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
...
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<versions-maven-plugin.version>2.2</versions-maven-plugin.version>
</properties>

以后我们导入依赖默认是不需要写版本(没有在dependencies里面管理的依赖自然需要声明版本号)

4.2 导入的依赖

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

spring-boot-starter-web

  • spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件
  • 更多启动器
  • Spring Boot将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目里引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

4.3 主程序类,主入口类

@SpringBootApplication

Spring Boot应用标注在某个类上说明这个类是Spring Boot的主配置类,Spring Boot就应该运行这个类的main方法来启动Spring Boot应用

点击这个注解(annotation)查看源码

@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,
attribute = "exclude"
)
Class<?>[] exclude() default {}; @AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "excludeName"
)
String[] excludeName() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}

解析

@SpringBootConfiguration // spring boot定义的注解

Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类

点击这个注解(annotation)查看源码

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

解析

@Configuration // spring定义的注解

配置类上来标注这个注解

配置类---配置文件:配置类也是容器中的一个组件:@Commponent

@EnableAutoConfiguration

开启自动配置功能

以前我们需要配置的东西,Spring Boot帮我们自动配置,这个注解告诉Spring Boot开启自动配置功能,这样自动配置才能生效

点击这个注解(annotation)查看源码

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class<?>[] exclude() default {}; String[] excludeName() default {};
}

解析

@AutoConfigurationPackage

自动配置包

点击这个注解(annotation)查看源码

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

解析

@Import({Registrar.class})

Spring的底层注解@import,给容器中导入一个组件

导入的组件由Registrar.class

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
...
// metadata是注解的原信息
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry
registry) {
AutoConfigurationPackages.register(registry, (new AutoConfigurationPackages.PackageImport(
metadata)).getPackageName());
} ...
}

将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器

@Import({EnableAutoConfigurationImportSelector.class})

给容器中导入组件,这是导入那些组件的选择器

点击这个注解(annotation)查看源码

/** @deprecated */
@Deprecated
public class EnableAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
public EnableAutoConfigurationImportSelector() {
} protected boolean isEnabled(AnnotationMetadata metadata) {
return this.getClass().equals(EnableAutoConfigurationImportSelector.class) ? (Boolean
)this.getEnvironment().getProperty("spring.boot.enableautoconfiguration", Boolean.class, true) : true;
}
}

然后再查看其父类的源码

里面有一个selectImports方法,将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中

会给容器中导入非常多的自动配置类(xxxAutoConfiguration):就是给容器中这个场景所需要的所有组件,并配置好这些组件

有了自动配置类,免去了我们手动编写配置注入功能组件等的工作

selectImports会调用该类里的getCandidateConfigurations方法

getCandidateConfigurations又调用了SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, classLoader)

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作

以前我们需要自己配置的东西,自动配置类帮我们

J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar

5、使用Spring Initializer快速创建Spring Boot项目

IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;

选择我们需要的模块,向导会联网创建Spring Boot项目;

默认生成的Spring Boot项目:

  • 主程序已经生成好了,我们只需要添加自己的逻辑即可

  • resources文件夹中的目录结构

    • static:保存所有的静态资源:js, css, images
    • templates:保存所以的模板页面:(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面),可以使用官方推荐模板引擎(Freemarker, Thymeleaf)
    • application.properties:Spring Boot应用的配置文件,虽然Spring Boot有默认配置,但我们可以修改一些默认配置

6、更多精彩

等多精彩,访问我的个人博客吧!

Spring Boot入门,源码解析的更多相关文章

  1. spring boot @Value源码解析

    Spring boot 的@Value只能用于bean中,在bean的实例化时,会给@Value的属性赋值:如下面的例子: @SpringBootApplication @Slf4j public c ...

  2. Spring Boot 启动源码解析结合Spring Bean生命周期分析

    转载请注明出处: 1.SpringBoot 源码执行流程图 2. 创建SpringApplication 应用,在构造函数中推断启动应用类型,并进行spring boot自动装配 public sta ...

  3. Spring Security 解析(七) —— Spring Security Oauth2 源码解析

    Spring Security 解析(七) -- Spring Security Oauth2 源码解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因 ...

  4. Feign 系列(05)Spring Cloud OpenFeign 源码解析

    Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...

  5. 异步任务spring @Async注解源码解析

    1.引子 开启异步任务使用方法: 1).方法上加@Async注解 2).启动类或者配置类上@EnableAsync 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就 ...

  6. Spring Security 访问控制 源码解析

    上篇 Spring Security 登录校验 源码解析  分析了使用Spring Security时用户登录时验证并返回token过程,本篇分析下用户带token访问时,如何验证用户登录状态及权限问 ...

  7. Spring @Import注解源码解析

    简介 Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内.而在Spring 3.0之后提供了JavaConfig的方式,也就是将IO ...

  8. api网关揭秘--spring cloud gateway源码解析

    要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...

  9. Spring Boot缓存源码分析

    前言 项目里面要增加一个应用缓存,原本想着要怎么怎么来整合ehcache和springboot,做好准备配置这个配置那个,结果只需要做三件事: pom依赖 写好一个ehcache的配置文件 在boot ...

  10. Spring容器启动源码解析

    1. 前言 最近搭建的工程都是基于SpringBoot,简化配置的感觉真爽.但有个以前的项目还是用SpringMvc写的,看到满满的配置xml文件,却有一种想去深入了解的冲动.折腾了好几天,决心去写这 ...

随机推荐

  1. 【模式识别与机器学习】——最大似然估计 (MLE) 最大后验概率(MAP)和最小二乘法

    1) 极/最大似然估计 MLE 给定一堆数据,假如我们知道它是从某一种分布中随机取出来的,可是我们并不知道这个分布具体的参,即“模型已定,参数未知”.例如,我们知道这个分布是正态分布,但是不知道均值和 ...

  2. 【模式识别与机器学习】——PCA与Kernel PCA介绍与对比

    PCA与Kernel PCA介绍与对比 1. 理论介绍 PCA:是常用的提取数据的手段,其功能为提取主成分(主要信息),摒弃冗余信息(次要信息),从而得到压缩后的数据,实现维度的下降.其设想通过投影矩 ...

  3. leetcode刷题笔记-1. 两数之和(java实现)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  4. C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...

  5. 使用Tensorflow搭建自编码器(Autoencoder)

    自编码器是一种数据压缩算法,其中数据的压缩和解压缩函数是数据相关的.从样本中训练而来的.大部分自编码器中,压缩和解压缩的函数是通过神经网络实现的. 1. 使用卷积神经网络搭建自编码器 导入MNIST数 ...

  6. 性能分析(6)- 如何迅速分析出系统 CPU 的瓶颈在哪里

    性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前言 在做性能测试时,我们会需要对 Linux 系 ...

  7. Angular Datatable的一些问题

    这几天改bug中发现的一些问题,小结一下.从简单到复杂逐个讲. angular datatable实质上是对jquery库的包装,但包装后不太好用,定制功能比较麻烦. 1. 基本用法 最简单的用法,大 ...

  8. 【Flutter 实战】一文学会20多个动画组件

    老孟导读:此篇文章是 Flutter 动画系列文章第三篇,后续还有动画序列.过度动画.转场动画.自定义动画等. Flutter 系统提供了20多个动画组件,只要你把前面[动画核心](文末有链接)的文章 ...

  9. 在.NET Core中使用MongoDB明细教程(3):Skip, Sort, Limit, Projections

    到目前为止,我们已经讨论了创建文档, 检索文档,现在让我们来研究一下文档排序,指定要跳过或限制返回的文档数量,以及如何进行投影.此篇文章中的实例代码摘录自原文,未像前几篇文章一样进行实际代码的验证. ...

  10. 【API进阶之路】逆袭!用关键词抽取API搞定用户需求洞察

    摘要: 老大说,我这份用关键词抽取API搞定的用户需求洞察报告,简直比比市场调研的科班人士做得还好. 最近这半个月的午饭,那可是相当不错,市场老大天天请吃饭,不是外面下馆子,就是从家带饺子.说是感谢我 ...