Spring Boot 1. 总览-核心特性

总览-核心特性
PDF 版本:第一节 系列总览-核心特性.pdf

Spring Boot 三大特性

组件 自动 装配: Web MVC , Web Flux ,JDBC 等

  • 激活: @EnableAutoConfiguration
  • 配置: /METE-INF/spring.factories (元信息目录,工厂机制,key value 形式)
  • 实现: XXXAutoConfiguration

嵌入式的 Web 容器: Tomcat , Jetty 以及 Undertow

  • Web Servlet: Tomcat , Jetty 和 Undertow
  • Web Reactive: Netty Web Server

生产准备特性: 指标 , 健康检查 , 外部配置化

  • 指标(Metrics): /actuator/metrics
  • 健康检查(Health Check): /actuator/health
  • 外部配置化(Externalized Configuration): /actuator/configprops

Web 应用

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

传统 Servlet 应用

Servlet 组件: Servlet , Filter , Listener

  • Servlet
    • 实现
      • @WebServlet
      • HttpServlet
      • 注册
    • URL 映射
      • @WebServlet(urlPatterns = "/my/servlet")
    • 注册
      • @ServletComponentScan(basePackages = "com.example.demo.web.servlet")
  • Filter
  • Listener  (在 Servlet 里面的一种事件监听者模式的实现. ServletContextListener , RequestListener)

Servlet 注册: Servlet 注解 , Spring Bean , RegistrationBean

Servlet 注解
  • @ServletComponentScan
    • @WebServlet
    • @WebFilter
    • @WebListener
Spring Bean
  • @Bean
    • Servlet
    • Filter
    • Listener
RegisterationBean
  • ServletRegisteration
  • FilterRegisteration
  • ServletListenerRegisteration

异步非阻塞: 异步 Servlet , 非阻塞 Servlet (Servlet 3.0 )

异步 Servlet
  • javax.serlvet.ServletRequest#startAsync()
  • javax.serlvet.AsyncContext
非阻塞 Servlet
  • javax.serlvet.ServletInputStream#setReadListener
    • javax.serlvet.ReadListener
  • javax.serlvet.ServletOutputStream#setWriteListener
    • javax.servlet.WriteListener

Spring Web MVC 应用

Web MVC 视图: 模板引擎 , 内容协商 , 异常处理等

  • ViewResolver
  • View
模板引擎
  • Thymeleaf
  • Freemarker
  • JSP
内容协商
  • ContentNegotiaionConfigurer
  • ContentNegotiaionStrategy
  • ContentNegotiaionViewResolver
异常处理
  • @ExceptionHandler
  • HandlerExceptionResolver
    • ExceptionHandlerExceptionResolver
  • BasicErrorController (Spring Boot)

Web MVC REST: 资源服务 , 资源跨域 , 服务发现等

资源服务
  • @RequestMapping
    • @GetMapping (查)
    • @PostMapping (增)
    • @PutMapping (改)
    • @DeleteMapping (删)
  • @ResponseBody
  • @RequestBody
资源跨域
  • CrossOrigin
  • WebMvcConfigurer#addCorsMappings
  • 传统解决方案
    • iFrame
    • JSONP
服务发现
  • HATEOS

Web MVC 核心:   核心架构 , 处理流程 , 核心组件

核心架构
处理流程
核心组件
  • DispatcherServlet
  • HandlerMapping
  • HandlerAdapter
  • ViewResolver

Spring Web Flux 应用

Reactor 基础

Java Lambda
Mono
Flux

Web Flux 核心

Web MVC 注解兼容
  • @Controller
  • @RequestMapping
  • @ResponseBody
  • @RequestBody
函数式声明
  • RouterFunction
异步非阻塞
  • Servlet 3.1
  • Netty Reactor

使用场景

页面渲染
REST 应用
性能测试

Web Server 应用

切换 Web Server

切换其他 Servlet 容器
  • Tomcat -> Jetty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions> <!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency> <!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
替换 Servlet 容器
  • WebFlux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--        <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- <exclusions>-->
<!-- <!– Exclude the Tomcat dependency –>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- <!– Use Jetty instead –>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-jetty</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

自定义 Servlet Web Server

  • WebServerFactoryCustomizer

自定义 Reactive Web Server

  • ReactiveWebServerFactoryCustomizer

数据相关

关系型数据

JDBC

依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
数据源
  • javax.sql.DataSource
JdbcTemplate
自动装配
  • DataSourceAutoConfiguration

JPA

依赖
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
实体映射关系
  • @javax.persistence.OneToOne
  • @javax.persistence.OneToMany
  • @javax.persistence.ManyToOne
  • @javax.persistence.ManyToMany
实体操作
  • javax.persistence.EntityManager
自动装配
  • HibernateJpaAutoConfiguration

事务(Transaction)

依赖
1
2
3
4
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
Spring 事务抽象
  • PlatformTransactionManager
JDBC 事务处理
  • DataSourceTransactionManager
自动装配
  • TransactionAutoConfiguration

功能扩展

Spring Boot 应用

SpringApplication

失败分析
  • FailureAnalysisReporter
应用特性
  • SpringApplication Fluent API
Spring Boot 配置
  • 外部化配置
    • ConfigurationProperty
  • @Profile
  • 配置属性
    • PropertySources

Spring Boot Starter

运维管理

Spring Boot Actuator

依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

端点(Endpoints)

Web Endpoints
JMX Endpoints

健康检查(Health Checks)

Health
HealthIndicator

指标(Metrics)

内建 Metrics
  • Web Endpoint : /actuator/metrics
自定义 Metrics

Spring Boot 1. 总览-核心特性
https://blog.josway.cc/2022/04/25/yuque/Spring Boot 1. 总览-核心特性/
作者
JOSWAY
发布于
2022年4月25日
许可协议