Springboot学习笔记(一)
Spring Boot三大特性
组件自动装配 :Web MVC,Web Flax,JDBC
嵌入式Web容器:Tomcat,Jetty,Undertow
生产准备特性:指标,健康检查,外部化配置
组件自动装配
激活: @EnableAutoConfiguration
配置: /META-INF/spring.factories
路径:
org\springframework\boot\spring-boot\2.1.1.RELEASE\spring-boot-
2.1.1.RELEASE.jar!\META-INF\spring.factories
以键值对形式配置:
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
实现: XXXAutoConfiguration
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration
嵌入式Web容器
Web Servlet 容器:Tomcat,Jetty,Undertow
Web Reactive 容器:Netty Web Server
生产准备特性
用于监控管理应用相关特性
指标:/actuator/metrics (监控cpu,内存,磁盘等利用率)
健康检查:/actuator/health (磁盘,数据库健康检查)
外部化配置:/actuator/configprops
Web应用
传统Servlet应用
- Servlet组件:Servlet,Filter,Listener
- Servlet注册:Servlet注解,Spring Bean,RegistrationBean
- 异步非阻塞:异步Servlet,非阻塞Servlet
Servlet组件
- 实现Servlet注解方式的注册
- @WebServlet注解
URL映射 @WebServlet(urlPatterns = "/my/servlet")
- 继承 HttpServlet
- 注册(启动类添加包扫描注解)
@ServletComponentScan(basePackages = "com.imooc.diveinspringboot.web.servlet")
@WebServlet(urlPatterns = "/my/servelt")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("servelt");
}
}
异步非阻塞
异步Servelt
- javax.servlet.ServletRequest#startAsync() javax.
- servlet.AsyncContext
//开启异步 asyncSupported = true
@WebServlet(urlPatterns = "/asyservlet",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("servelt");
//触发完成
asyncContext.complete();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
Spring Web MVC 应用
Web MVC 视图
- ViewResolver
- View
模板引擎
- Thymeleaf
- Freemarker
- JSP
内容协商
- ContentNegotiationConfigurer
- ContentNegotiationStrategy
- ContentNegotiatingViewResolver
异常处理
- @ExceptionHandler
- HandlerExceptionResolver
- ExceptionHandlerExceptionResolver
- BasicErrorController (Spring Boot)
Web MVC REST
资源服务
- @RequestMapping
- @GetMapping
@RequestMapping(
method = {RequestMethod.GET}
)
public @interface GetMapping {
@AliasFor(//委派方式
annotation = RequestMapping.class
)
- @ResponseBody
- @RequestBody
资源跨域
- 1.注解驱动 CrossOrigin
@AliasFor("origins")
String[] value() default {};
@AliasFor("value")
String[] origins() default {};
String[] allowedHeaders() default {};
String[] exposedHeaders() default {};
RequestMethod[] methods() default {};
String allowCredentials() default "";
long maxAge() default -1L;
}
- 2.接口编程 WebMvcConfigurer #addCorsMappings
default void addCorsMappings(CorsRegistry registry) {}
- 传统解决方案(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
视频p=10 00:18