SpringBoot 默认打包方式为jar包,且可以自启动,就是因为它内嵌了Servlet容器。

SpringBoot 默认使用嵌入式Servlet容器,SpringBoot 2.2.5 默认是 Tomcat 9.0.19,(SpringBoot 默认支持三种Servlet容器:tomcat,jetty,undertow)。

使用SSM的时候,我们可以根据自己的需求来定制容器的相关参数,那么在SpringBoot应用中我们要如何配置Servlet容器呢?

1、配置和修改 Servlet容器的相关配置

嵌入的Servlet容器,没有server.xml 配置文件修改配置。SpringBoot 提供了对应方式来配置:
(1) 在SpringBoot 的全局配置文件中配置(在 ServerProperties 类里查询可以所有能配置属性)
        1)server.xxxx 属性   配置有关Servlet容器的通用配置
        2)server.tomcat.xxxx 属性专门配置tomcat有关的配置
        3)其他两个Servlet容器以此类推:server.jetty.xxxx ,server.undertow.xxxx

server:port: 80servlet:context-path: /aa# tomcat 相关配置tomcat:uri-encoding: UTF-8

   

(2)编写一个 EmbeddedServletContainerCustomizer,它是嵌入式servlet容器的定制器,可以用它来修改servlet的配置.

在SpringBoot2.0以上版本中, EmbeddedServletContainerCustomizer 被 WebServerFactoryCustomizer 代替,因此要使用下面这种方式来实现:

@Configuration
public class WebConfig {/***  修改 Servlet 容器配置* @return*/@Beanpublic WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryWebServerFactoryCustomizer(){return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {@Overridepublic void customize(ConfigurableWebServerFactory factory) {factory.setPort(8002);}};}}

一、SpringBoot 注册 servlet 三大组件

在以前的项目开发中,因为一般web引用都是war包的方式,我们可以在web.xml文件中定义自己需要的 servlet, filter, listener 组件,在SpringBoot中 默认为jar包打包,所以没有web.xml,因此在SpringBoot使用servet的容器组装要按照下面的方式来:

1、ServletRegistrationBean:注册 Servlet

创建自定义servlet类,继承HttpServlet , 然后再配置类中注入

public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");resp.getWriter().write("自己定义的MyServlet...");}
}
@Configuration
public class WebConfig {/*** 注册 Servlet* @return*/@Beanpublic ServletRegistrationBean myServlet(){return new ServletRegistrationBean(new MyServlet(),"/myServlet"); // 参数(servlet对象, 设置映射的路径)}
}

    

 2、FilterRegistrationBean:注册Filter

public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("MyFilter 过滤器执行了...");filterChain.doFilter(servletRequest, servletResponse);}@Overridepublic void destroy() {}
}
    @Beanpublic FilterRegistrationBean myFilter(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());//设置要拦截的请求路径filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet", "/index"));return filterRegistrationBean;}

3、ServletListenerRegistrationBean:注册Listener

public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");resp.getWriter().write("自己定义的MyServlet...");}
}
    @Beanpublic ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean servletListenerRegistrationBean = newServletListenerRegistrationBean(new MyListener());return servletListenerRegistrationBean;}

SpringBoot 在自动配置SpringMVC的时候,会自动注册SpringMVC前端控制器:DispatcherServlet,该控制器主要在DispatcherServletAutoConfiguration 自动配置类中进行注册的。

查看 DispatcherServletAutoConfiguration 这个类:

@AutoConfigureOrder(-2147483648)
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET
)
@ConditionalOnClass({DispatcherServlet.class})
@AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class})
public class DispatcherServletAutoConfiguration {public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";//other code...@Configuration@Conditional({DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition.class})@ConditionalOnClass({ServletRegistration.class})@EnableConfigurationProperties({WebMvcProperties.class})@Import({DispatcherServletAutoConfiguration.DispatcherServletConfiguration.class})protected static class DispatcherServletRegistrationConfiguration {private final WebMvcProperties webMvcProperties;private final MultipartConfigElement multipartConfig;public DispatcherServletRegistrationConfiguration(WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfigProvider) {this.webMvcProperties = webMvcProperties;this.multipartConfig = (MultipartConfigElement)multipartConfigProvider.getIfAvailable();}@Bean(name = {"dispatcherServletRegistration"})@ConditionalOnBean(value = {DispatcherServlet.class},name = {"dispatcherServlet"})public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath());//默认拦截 / 所有请求,包括静态资源,但是不拦截jsp请求;/*会拦截jsp//可以通过修改server.servlet-path来修改默认的拦截请求registration.setName("dispatcherServlet");registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());if (this.multipartConfig != null) {registration.setMultipartConfig(this.multipartConfig);}return registration;}}
}

二、SpringBoot 如何切换这三个Servlet容器:

tomcat:开源,SpringBoot默认,使用比较多

jetty:小Serlvet容器,适合做长链接,也就是链接上就不断,长连接多用于操作频繁,点对点的通讯,而且连接数不能太多情况,很多人开发测试的时候很喜欢用,因为它启动比tomcat要快很多

undertow:是一个非阻塞的Servlet容器,但是它不支持JSP。

Spring Boot 默认使用Tomcat,一旦引入 spring-boot-starter-web 模块,就默认使用Tomcat容器

切换其他Servlet容器,只要修改项目的pom.xml文件即可:

​      1)将tomcat依赖移除掉

​      2)引入其他Servlet容器依赖

注意切换了Servlet容器后,只是以server开头的配置不一样外,其他都类似

1、引入jetty:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><artifactId>tomcat-embed-core</artifactId><groupId>org.apache.tomcat.embed</groupId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>

2、引入undertow:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><artifactId>tomcat-embed-core</artifactId><groupId>org.apache.tomcat.embed</groupId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency>

ends ~

SpringBoot 配置嵌入式Servlet容器(tomcat,jetty,undertow)相关推荐

  1. SpringBoot配置嵌入式Servlet容器

    SpringBoot默认使用的是Tomcat作为嵌入式的Servlet容器,那么肯定会和外置的Tomcat有区别,那么就这些区别来谈一谈SpringBoot中对于容器的一些配置操作 如何定制和修改Se ...

  2. SpringBoot之配置嵌入式Servlet容器

    1.概述 文章目录 1.概述 2.如何修改SpringBoot的默认配置 3.定制和修改Servlet容器的相关配置 4.注册Servlet三大组件 5.替换为其他嵌入式Servlet容器 6.嵌入式 ...

  3. Spring boot配置嵌入式Servlet容器

    Servlet容器 项目,打成war包,放在Tomcat启动 Tomcat就是一个Servlet容器 Spring boot 默认的嵌入了Servlet容器Tomcat 打开项目pom文件 右键Dia ...

  4. springboot(七) 配置嵌入式Servlet容器

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  5. Spring Boot配置嵌入式Servlet容器的两种方法

    一.前言 SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 二.如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[ ...

  6. Spring Boot中配置嵌入式Servlet容器修改配置

    目录 基本概念 演示及源码 基本概念 如何修改Spring Boot的默认配置: 1. Spring Boot在自动配置很多组建的时候,先看容器中有没有用户自己配置的(@Bean,@Component ...

  7. idea servlet自动配置web.xml_Spring Boot学习04_嵌入式Servlet容器自动配置原理

    在Spring Boot的自动配置包下,找到web模块中的servlet文件夹下的ServletWebServerFactoryConfiguration类 一.嵌入式Servlet容器默认的配置原理 ...

  8. 嵌入式Servlet容器

    配置嵌入式Servlet容器 ##Spring Boot里面内置了嵌入式的Servlet容器(tomcat) 点击pom.xml->右键->Diagrams->show Depend ...

  9. SpringBoot 嵌入式Servlet容器

    一.嵌入式Servlet容器 切换嵌入式Servlet容器 默认支持的webServer :Tomcat, Jetty, or Undertow ServletWebServerApplication ...

最新文章

  1. 《Typecript 入门教程》 2、访问控制符:public、private、protected、readonly
  2. C++ auto 关键字的使用
  3. 高级Java服务端工程师要求
  4. nohup-真正的Shell后台运行
  5. ubuntu18.04下安装tomcat8.5
  6. 论文学习15-Table Filling Multi-Task Recurrent Neural Network(联合实体关系抽取模型)
  7. idea 注释中 类 跳转_javaSE第一部分 数据类型、idea快捷键
  8. oppo手机计算机,OPPO手机助手
  9. 最全2019 AI/计算机/机器人顶会时间表来了,共收录36场会议,投稿冲鸭!
  10. labelme批量转换json
  11. Hive复杂数据类型之array
  12. Linux下c编程设置串口属性和读写串口操作说明总结
  13. WAS7.0安装补丁升级程序无法替换文件 java/docs/autorun.inf解决办法
  14. python read_csv dtype_Pandas read_csv low_memory和dtype选项
  15. Enterprise Library 4.1 Configuration Sources 图文笔记
  16. 计算机网络安全 单词
  17. 五年饮冰,难凉热血”,一名专科生的求学历程
  18. 「笔耕不辍」zookeeper集群之间如何通讯
  19. 按键精灵的5级开发认证,笔试题参考
  20. 科学计算机设计总结,科学计算器课程设计报告

热门文章

  1. tf.estimator.EstimatorSpec讲解
  2. cURL error 18: transfer closed with xxxxxxx bytes remaining to read
  3. 压力测试工具tsung
  4. Nvidia AGX Xavier MAX9286 GMSL 载板
  5. FL studio 20简易入门教程 -- 第九篇 -- 完整编曲流程
  6. mysql 视图的作用
  7. 树莓派:双色LED灯实验
  8. 写服务器node实际项目,基于node搭建服务器,写接口,调接口,跨域的实例
  9. 【Matlab系列】常用模拟和数字通信系统仿真及Matlab实现
  10. 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?用循环嵌套结构完成。(数字组合)