spring boot配置Servlet容器
Spring boot 默认使用Tomcat作为嵌入式Servlet容器,只需要引入spring-boot-start-web依赖,默认采用的Tomcat作为容器

01 定制和修改Servlet容器的相关配置(ServerProperties是EmbeddedServletContainerCustomizer的子类)
server.port=8080
server.context-path=/ # tomcat相关设置
server.tomcat.uri-encoding=UTF-8
也可以编写EmbeddedServletContainerCustomizer(嵌入式的Servlet容器定制器),来修改servlet容器的配置
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
//定制嵌入式的Servlet容器相关的属性配置
return container -> container.setPort(8083);
}
2 注册Servlet容器的三大组件(Servlet Filter Listener)
spring boot默认采用是以jar包的形式启动嵌入式的servlet容器,从而启动Springboot的web应用,没有web.xml,当然也可以使用注解的方式(@WebServlet,@WebListener,@WebFilter),现在使用Spring boot作为框架,如果编写三大组件,则需要使用配置的方式进行注册
01 ServletRegistrationBean:注册Servlet
//Servlet定义
public class MyServlet extends HttpServlet { @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("这是一个servlet请求...");
}
} //Servlet注册
@Configuration
public class MyServletConfig { //注册Servlet
@Bean
public ServletRegistrationBean myServlet(){
return new ServletRegistrationBean(new MyServlet(), "/myServlet");
}
}
02 FilterRegistrationBean:注册Filter
//Filter定义
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter process...");
//放行
chain.doFilter(request, response);
} @Override
public void destroy() { }
} //Filter注册
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
return bean;
}
ServletListenerRegistrationBean:注册Listener
//Listener定义
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized...web启动");
} @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed...web销毁");
}
} //Listener注册
@Bean
public ServletListenerRegistrationBean myListener(){
return new ServletListenerRegistrationBean<>(new MyListener());
}
最熟悉的是,spring boot 在自动配置springMVC,会自动注册SpringMVC前端控制器:DispatcherServlet,该控制器主要在DispatcherServletAutoConfiguration自动配置类中进行注册
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
public class DispatcherServletAutoConfiguration { //other code... public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet"; private String servletPath = "/"; @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
//默认拦截 / 所有请求,包括静态资源,但是不拦截jsp请求;/*会拦截jsp
//可以通过修改server.servlet-path来修改默认的拦截请求
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
}
3 其他Servlet容器
Spring boot默认支持Tomcat,Jetty 和Undertow作为底层容器

而Spring boot默认使用Tomcat,一旦引入spring-boot-starter-web模块,就默认使用Tomcat
切换其其他Servlet容器
将tomcat依赖移除同事引入其他Servlet容易
引入jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
引入undertow
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
参考文章 https://blog.csdn.net/caychen/article/details/80344372
这里对于常见的容器jetty和tomcat做一个简单对比
jetty 更轻量级,更加灵活,可插拔和可扩展,其架构是基于Handler来实现的,可以同时处理大量长时间连接,默认采用NIO,在处理静态资源性能较高
Tomcat 默认采用BIO,基于容器设计的,不易扩展,对JavaEE和Servlet的支持更加全面,很多特性直接集成
spring boot配置Servlet容器的更多相关文章
- 【串线篇】spring boot嵌入式Servlet容器自动配置原理
EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置? @AutoConfigureOrder(Ordered.HIGHEST_PREC ...
- 【串线篇】spring boot嵌入式Servlet容器启动原理;
什么时候创建嵌入式的Servlet容器工厂?什么时候获取嵌入式的Servlet容器并启动Tomcat: 获取嵌入式的Servlet容器工厂: 1).SpringBoot应用启动运行run方法 2).r ...
- spring boot整合servlet、filter、Listener等组件方式
创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...
- spring boot中servlet启动原理
启动过程及原理 1 spring boot 应用启动运行run方法 StopWatch stopWatch = new StopWatch(); stopWatch.start(); Configur ...
- spring boot 2.x 系列 —— spring boot 整合 servlet 3.0
文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...
- Spring Boot 容器选择 Undertow 而不是 Tomcat Spring Boot 内嵌容器Unde
Spring Boot 内嵌容器Undertow参数设置 配置项: # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程 # 不要设置过大,如果过大,启动 ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像。
背景:Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像. 原文地址 https://github.com/weibaohui/springboot ...
随机推荐
- QT两个窗口相互切换
信号(signals)与槽(slots)是QT重要机制,例子使用了C++11 lambda表达式进行了信号与槽的连接. 实现两个窗口通过点击按钮完成互相切换,注意子窗口只能发送信号,不能处理,所有信号 ...
- android系统中对ffmpeg封装最好的免费SDK
android系统中对ffmpeg封装最好的免费SDK; 无论个人还是公司,都免费商用, 欢迎下载. https://github.com/LanSoSdk/LanSoEditor_common 可能 ...
- Spring IOC(2)----如何注册bean定义
前面说到IOC容器在刷新之前的一些初始化工作,现在来看看在refresh()方法中,是怎样来加载注册我们自己的bean定义的. refresh()方法中有很多功能,从注释中就可以看出来 我们本次重点关 ...
- Spring Boot(四) Mybatis-MySql
Spring Boot(四) Mybatis-MySql 0.准备数据库表 -- ---------------------------- -- Table structure for person ...
- Java 学习笔记之 线程安全
线程安全: 线程安全的方法一定是排队运行的. public class SyncObject { synchronized public void methodA() { try { System.o ...
- Scala 学习笔记之集合(4)
集合的模式匹配操作: object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List( ...
- Docker service update更新不成功的问题
一.基本信息 1.Docker版本 [root@ip---- ~]# docker --version Docker version , build a872fc2f86 2.系统版本 [root ...
- ORM查询总结版
目录 概要 ORM常用字段 ORM基础 自定义一个插入类型,即固定长度 创建类终极版 多对多关系表创建 常用几个代码 参数 ORM与数据库代码对应的关系 外键使用分表很麻烦,要先删除主表后,再删除 不 ...
- orm加强版
目录 十三式 2式(针对外键查询优化) select_related和prefetch_related prefetch_related 查询返回值类型 不等式查询 关键字查询 时间查询 跨表查询 组 ...
- ACM-图论-同余最短路
https://www.cnblogs.com/31415926535x/p/11692422.html 一种没见过的处理模型,,记录一下,,主要是用来处理一个多元一次方程的解的数量的问题,,数据量小 ...