组件自动装配 :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 Servlet 容器:Tomcat,Jetty,Undertow
Web Reactive 容器:Netty Web Server
用于监控管理应用相关特性
指标:/actuator/metrics (监控cpu,内存,磁盘等利用率)
健康检查:/actuator/health (磁盘,数据库健康检查)
外部化配置:/actuator/configprops
- @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");
}
}
//开启异步 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();
}
});
}
}
@RequestMapping(
method = {RequestMethod.GET}
)
public @interface GetMapping {
@AliasFor(//委派方式
annotation = RequestMapping.class
)
@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;
}
default void addCorsMappings(CorsRegistry registry) {}