一、springboot的三大特性
1、组件自动装配:Web MVC、Web Flux、JDBC等
-激活:@EnableAutoConfiguration
-配置:/META-INF/spring.factories
-实现:XXXAutoConfiguration
2、嵌入式Web容器:Tomcat、Jetty以及UnderTow等
-Web Servlet: Tomcat、Jetty和Undertow
-Web Reactive:Netty Web Server
3、生产准备特性:指标、健康检查、外部化配置等
-指标:/actuator/metrics
-健康检查:/actuator/health
-外部化配置:/actuator/configprops
二、Web应用
1、传统Servlet应用
-Servlet组件:Servlet、Filter、Listener
-Servlet:实现、URL映射、注册
MyServlet.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* My Servlet
*/
@WebServlet(urlPatterns = "/my/servlet")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Hello,World!");
}
}
SpringBootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
public class DiveInSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(DiveInSpringBootApplication.class, args);
}
}
-Servlet注册:Servlet注解、Spring Bean、RegistrationBean
-异步非阻塞:异步Servlet、非阻塞Servlet
MyServlet.java
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* My Servlet
*/
@WebServlet(urlPatterns = "/my/servlet",asyncSupported = true)
public class MyServlet extends HttpServlet {
@Override
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();
}
});
}
}
2、Spring Web MVC应用
-Web MVC视图(viewResolver、view):模板引擎(Thymeleaf、Freemarker、JSP)、内容协商(ContentNegotiationConfigurer、ContentNegotiationStrategy、ContentNegotiatingViewResolver)、异常处理等(ExceptionHandler、HandlerExceptionResolver(ExceptionHandlerExceptionResolver)、BasicErrorController)
-Web MVC REST:资源服务(@RequestMapping(@GetMapping)、@ResponseBody、@RequestBody)、资源跨域(CrossOrigin、WebMvcConfigurer#addCorsMappings,传统解决方案(IFrame、JSONP))、服务发现(HATEOS)
-Web MVC核心:核心架构、处理流程、核心组件(DispatchServlet、HandlerMapping、HandlerAdapter、ViewResolver…)
3、Spring WebFlux应用 (since spring 5.x)
-Reactor基础:Java Lambda、Mono、Flux
-Web Flux核心:Web MVC注解兼容(@controller、@RequestMapping、@ResponseBody、@RequestBody…)、函数式声明(RouterFunction)、异步非阻塞(Servlet3.1 +、Netty Reactor)
-使用场景:页面渲染、Rest应用、性能测试
4、Web Server应用
-切换Web Server(将Tomcat切换为Jetty)
pom.xml
<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>
替换为WebFlux
<!--<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
webServletFactoryCustomizer
-自定义Reactive Web Server
三、数据相关
1、关系型数据
-JDBC:数据源,jdbcTemplate、自动装配
-JPA:实体映射关系、实体操作、自动装配
-事务:Spring事务抽象、JDBC事务处理、自动装配