springBoot核心技术概览
核心特性
组件自动装备(webMVC, web Flux,JDBC等)
- 激活: @EnableAutoConfiguration
- 配置: /META-INF/spring.factories
- 实现: XXXAutoConfiguration
嵌入式Web容器
- Web Servlet 容器:Tomcat,jetty和Undertow
- Web Reactive 容器:Netty web server
生产准备特性
- 指标(Metrics)/actuator/metrics
- 健康检查(Health Check)/actuator/health
- 外部化配置(Externalized Configuration)/actuator/configprops
web应用
传统 Servlet 应用
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Servlet 组件
- servlet
- 实现
- @WebServlet,使用此注解
- URL映射
@WebServlet(urlPatterns = "/my/servlet"}
- 注册,见下面
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
@WebServlet(urlPatterns = "/my/servlet")
public class MyServlet extends HttpServlet {}
Servlet注册
- servlet注解
- @ServletComponentScan +
- @WebServlet
- @WebFilter
- @WebListener
@SpringBootApplication
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
public class DiveInSpringBootApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DiveInSpringBootApplication.class)
.web(WebApplicationType.NONE)
.run(args);
SpringApplication.run(DiveInSpringBootApplication.class, args);
}
}
- Spring Bean
- RegistrationBean
- ServletRegistrationBean
- FilterRegistrationBean
- ServletListenerRegistrationBean
异步非阻塞
- 异步 Servlet
- javax.servlet.ServletRequest#startAsync()
- javax.servlet.AsyncContext
@WebServlet(urlPatterns = "/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 (IOException e) {
e.printStackTrace();
}
});
}
}
- 非阻塞 Servlet
- javax.servlet.ServletInputStream#setReadListener
- javax.servlet.ReadListener
- javax.servlet.ServletOutputStream#setWriteListener
- javax.servlet.WriteListener
Spring Web MVC 应用
Web MVC 视图
模板引擎
内容协商
- ContentNegotiationConfigurer
- ContentNegotiationStrategy
- ContentNegotiatingViewResolver
异常处理
- @ExceptionHandler
- HandlerExceptionResolver
* ExceptionHandlerExceptionResolver
- BasicErrorController (Spring Boot)
Web MVC REST
资源服务
- @RequestMapping
- @GetMapping
- @ResponseBody
- @RequestBody
资源跨域
- CrossOrigin
- WebMvcConfigurer#addCorsMappings
- 传统解决方案
服务发现
Web MVC 核心
核心架构
处理流程
核心组件
- DispatcherServlet
- HandlerMapping
- HandlerAdapter
- ViewResolver
Spring Web Flux 应用
Reactor 基础
Web Flux 核心
Web MVC 注解兼容
- @Controller
- @RequestMapping
- @ResponseBody
- @RequestBody
函数式声明
异步非阻塞
- Servlet 3.1 +
- Netty Reactor
使用场景
页面渲染
REST 应用
性能测试
http://blog.ippon.tech/spring-5-webflux-performance-tests
Web Server 应用
切换 Web Server
切换其他 Servlet 容器
- Tomcat -> Jetty;在pom文件中添加如下
<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-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
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
数据源
JdbcTemplate
自动装配
- DataSourceAutoConfiguration
JPA
依赖
<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)
依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
Spring 事务抽象
- PlatformTransactionManager
JDBC 事务处理
- DataSourceTransactionManager
自动装配
- TransactionAutoConfiguration