一.核心特性
1.Springboot三大特性
- 组件自动装配:Web MVC、Web Flux、JDBC等
- 嵌入式Web容器:Tomcat、Jetty以及Undertow
- 生产准备特性:指标、健康检查、外部化配置等
2.组件自动装配
- 激活:
@EnableAutoConfiguration
- 配置:
/META-INF/spring.factories
[可以使用默认的配置也可以在指定目录下配置] - 实现:XXXAutoConfiguration
3.嵌入式Web容器
- Web Servlet:Tomcat、Jetty、Undertow
- Web Reactive:Netty Web Server
4.生产准备特性
- 指标:/actuator/metrics【cpu、内存及磁盘等监控】
- 健康检查:/actuator/health
- 外部化配置:/actuator/configprops
5.传统Servlet应用
- Servlet组件:Servlet、Filter、Listener
- Servlet注册:Servlet注解、SpringBean、RegistrationBean
- 异步非阻塞:异步Servlet、非阻塞Servlet
二.传统Servlet应用
1.依赖
compile 'org.springframework.boot:spring-boot-starter-web'
2.servlet
-
实现与url映射
@WebServlet(urlPattern="/my/servlet")//urlPattern进行url映射 public class WebServlet extends HttpServlet{ //可以实现doGet或doPost方法 }
-
注册
- 在Application类上 注册
@ServletComponentScan(basePackage="com.test.web.servlet")
自定义的servlet实现包
- 在Application类上 注册
3.异步非阻塞
-
异步Servlet和非阻塞Servlet
-
使用AsyncContext的start方法编写异步内容
-
默认@WebServlet使用的是同步Servlet,需要使用
asyncSupported=true
使用异步Servlet -
实例代码
@WebServlet(urlPattern="/my/servlet",asyncSupported=true) public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { AsyncContext asyncContext = req.startAsync(); asyncContext.start(()->{ try{ resp.getWriter().println("Hello World"); //触发完成 asyncContext.complete(); } catch(Exception e){ e.printStackTrace(); } }); } }
-
三.Web应用
1.SpringWebMVC应用
- Web MVC 视图:模板引擎、内容协商、异常处理等
- Web MVC REST:资源服务、资源跨域、服务发现等
- Web MVC 核心:核心架构、处理流程、核心组件
2.Web MVC视图
- ViewResolver接口(视图处理器)
- View接口
- 模板引擎
- Thymeleaf
- Freemarker
- JSP
- 内容协商
- ContentNegotiationConfigurer
- ContentNegotiationStrategy
- ContentNegotiatingViewResolver
- 异常处理
@ExceptionHandler
HandlerExceptionResolver
ExceptionHandlerExceptionResolver
BasicErrorController
类主要用来显示基本的错误处理(错误时的白页)
3.Web MVC REST
- 资源服务
- @RequestMapping
- @GetMapping
- @ResponseBody
- @RequestBody
- @RequestMapping
- 资源跨域
- CrossOrign注解(告诉浏览器哪些内容可以跨域)
- WebMvcConfigurer接口中addCorsMappings方法(webmvc的实现方案)
- 服务发现
- HATEOS
4.Spring Web Flux应用
- Reactor基础:Java Lambda/Mono/Flux
- Web Flux核心:Web MVC注解、函数式编程(RouterFunction)、异步非阻塞(Servlet3.1/Netty Reactor)
- 使用场景:Web Flux优势和限制
5.Web Server应用
- 切换Web Server(从Tomcat切换成Jetty:exclude Tomcat,引入Jetty)
- 自定义Servlet Web Server(实现WebServerFactoryCustomizer接口)
- 自定义Reactive Web Server(实现ReactiveWebServerFactoryCustomizer接口)
6.数据相关
- 关系型数据
- JDBC:数据源、JDBCTemplate、自动装配
- 依赖
spring-boot-starter-jdbc
- 数据源
javax.sql.DataSource
/JdbcTemplate
- 自动装配
DataSourceAutoConfiguration
- 依赖
- JPA:实体映射关系、实体操作、自动装配
- 事务:Spring事务抽象、JDBC事务处理、自动装配
- 依赖
spring-tx
- Spring事务抽象
PlatformTransactionManager
- JDBC事务处理
DataSourceTransactionManager
- 自动装配
TransactionAutoConfiguration
- 依赖
- JDBC:数据源、JDBCTemplate、自动装配
7.功能扩展
- SpringBoot应用
- SpringApplication:失败分析(FailureAnalysisReporter)、应用特性(FluentAPI)、事件监听等
- SpringBoot配置:外部化配置(ConfigurationProperty)、@Profile、配置属性(PropertySources)
- SpringBootStarter:Starter开发、最佳实践
四.运维管理
1.Spring Boot Actuator
- 端点Endpoints:各类Web和JMX Endpoints
- 健康检查:Health、HealthIndicator
- 指标:内建Metrics、自定义Metrics