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 ...
随机推荐
- lcx端口转发
目录 0x01 正向端口转发 0x02 反向端口转发 0x03 msf正向shell 0x04 msf反向shell 注: 边界机器 win08 192.168.222.175 内网机器 win7 1 ...
- 接口是什么?接口长什么样?java的Interface
今天来看看java接口长哪样.接口是特殊抽象类. 一个子类只能继承一个抽象类(父类),所以就有接口这个特殊抽象类. 下面以一个电脑的USB为例: 定义接口标准 public interface USB ...
- samba + OPENldap 搭建文件共享服务器
samba + OPENldap 搭建文件共享服务器 这里我使用的是 samba(文件共享服务) v4.9.1 + OPENldap(后端数据库软件) v2.4.44 + smbldap-tools( ...
- Spring MVC-从零开始-文件上传(未完待续)
Spring MVC-从零开始-文件上传(未完待续)
- mybatis批量插入应用
一.foreach简单介绍 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sepa ...
- [Week 2][Guarantee of PLA] the Correctness Verification of PLA
Conditions: For the data set D, there exists a $\displaystyle W_{f}$ which satisfies that for every ...
- java架构之路-(spring源码篇)springIOC容器源码解析(上)
我们这次来叭叭一下Spring的源码,这次博客主要来说说Spring源码,先粗略的撸一遍,下篇博客选几个重点去说,由于过于复杂,我也是看了一点点,我们先来过一遍源码,然后上流程图,最后我们再回头总结一 ...
- netty源码解解析(4.0)-23 ByteBuf内存管理:分配和释放
ByteBuf内存分配和释放由具体实现负责,抽象类型只定义的内存分配和释放的时机. 内存分配分两个阶段: 第一阶段,初始化时分配内存.第二阶段: 内存不够用时分配新的内存.ByteBuf抽象层没有定义 ...
- 什么是ECMAScript、什么又是ECMA?
转载:针对于ECMA5Script .ECMAScript6.TypeScript的认识 什么是ECMAScript.什么又是ECMA? Ecma国际(Ecma International)是一家国际 ...
- linux下修改python版本号
修改python版本 1.查看已安装版本: /home/user$ whereis python 2.在其 home 目录下创建一个 alias(别名) user@ubuntu:/home/user$ ...