Spring Boot 深度实践 – 系列总览

核心特性


组件自动装配

激活: @EnableAutoConfiguration

配置: /META-INF/spring.factories

实现: XXXAutoConfiguration

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

Web MVC

依赖


    org.springframework.boot
    spring-boot-starter-web

嵌入式Web容器

  • Web Servlet 容器

  • Web Reactive 容器

生产准备特性

  • 指标(Metrics)
  • 健康检查(Health Check)
  • 外部化配置(Externalized Configuration)

Web 应用

传统 Servlet 应用

依赖


    org.springframework.boot
    spring-boot-starter-web

Servlet 组件

  • Servlet
    • 实现
      • @WebServlet
      • HttpServlet
      • 注册
    • URL 映射
      • @WebServlet(urlPatterns = "/my/servlet")
    • 注册
      • @ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
  • Filter
  • Listener

Servlet 注册

Servlet 注解

  • @ServletComponentScan +
  • @WebServlet
  • @WebFilter
  • @WebListener

Spring Bean

  • @Bean +
  • Servlet
  • Filter
  • Listener

RegistrationBean

  • ServletRegistrationBean
  • FilterRegistrationBean
  • ServletListenerRegistrationBean

异步非阻塞

异步Servlet

  • javax.servlet.ServletRequest#startAsync()

  • javax.servlet.AsyncContext

非阻塞Servlet

  • javax.servlet.ServletInputStream#setReadListener
    • javax.servlet.ReadListener
  • javax.servlet.ServletOutputStream#setWriteListener
    • javax.servlet.WriteListener

Spring Web MVC应用

Web MVC视图

  • ViewResolver
  • View

模版引擎

  • Thymeleaf
  • Freemarker
  • JSP

内容协商

  • ContentNegotiationConfigurer
  • ContentNegotiationStrategy
  • ContentNegotiatingViewResolver

异常处理

  • @ExceptionHandler
  • HandlerExceptionResolver
    • ExceptionHandlerExceptionResolver
  • BasicErrorController(SpringBoot)

Web MVC REST

资源服务

  • @RequestMapping
    • @GetMapping
  • @ResponseBody
  • @RequestMapping

资源跨域

  • CrossOrigin
  • WebMvcConfigurer#addCorsMappings
  • 传统解决方案
    • IFrame
    • JSONP

服务发现

  • HATEOS

Web MVC 核心

核心架构

处理流程

核心组件

  • DispatcherServlet
  • HandlerMapping
  • HandleAdapter
  • ViewResolver

Spring Web Flux 应用

  • Reactor 基础
  • Java Lambda
  • Mono
  • Flux

Web Flux核心

Web MVC注解兼容

  • @Controller
  • @RequestMapping
  • @ResponseBody
  • @ResquestBody
  • ..

函数式声明

  • RouterFunction

异步非阻塞

  • Servlet 3.1+
  • Netty Reactor

使用场景

  • 页面渲染
  • REST应用
  • 性能测试
    http://blog.ippon.tech/spring-5-webflux-performance-tests/

Web Server应用

切换Web Server

切换其他Servlet容器

  • Tomcat-> Jetty

    org.springframework.boot
    spring-boot-starter-web
    
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        
    



    org.springframework.boot
    spring-boot-starter-jetty


替换Servlet容器

  • WebFlux

    
    
    
        
        
  
  
                
            
        
    
    
    
        
        
    

    org.springframework.boot
    spring-boot-starter-webflux

自定义 Servlet Web Server

  • WebServerFactoryCustomizer

自定义 Reactive Web Server

  • ReactiveWebServerFactoryCustomizer

数据相关

关系型数据

JDBC

依赖


    org.springframework.boot
    spring-boot-starter-jdbc

数据源

  • javax.sql.DataSource

自动装配

  • DataSourceAutoConfiguration

JPA

依赖


    org.springframework.boot
    spring-boot-starter-data-jpa

实体映射关系

  • @javax.persistence.OneToOne
  • @javax.persistence.OneToMany
  • @javax.persistence.ManyToOne
  • @javax.persistence.ManyToMany
  • ..

实体操作

  • javax.persistence.EntityManager

自动装配

  • HibernateJpaAutoConfiguration

事务(Transaction)

依赖


  org.springframework
  spring-tx

Spring 事务抽象

  • PlatformTransactionManager

JDBC 事务处理

  • DataSourceTransactionManager

自动装配

  • TransactionAutoConfiguration

功能扩展

Spring Boot应用

失败分析

  • FailureAnalysisReporter

应用特性

  • SpringApplication Fluent API

Spring Boot配置

  • 外部化配置
    • ConfigurationProperty
  • @Profile
  • 配置属性
    • PropertySources

Spring Boot Starter

运维管理

Spring Boot Actuator

依赖


    org.springframework.boot
    spring-boot-starter-actuator

端点(Endpoints)

  • Web Endpoints
  • JMX Endpoints

健康检查(Health Checks)

  • Health
  • HealthIndicator

指标(Metrics)

内建Metrics

  • Web Endpoints:/actuator/metrics

自定义Metrics

你可能感兴趣的:(Spring Boot 深度实践 – 系列总览)