概述SpringBoot支持嵌入式Tomcat、Jetty和Undertow服务器。大多数开发人员使用适当的“启动器”来获得完全配置的实例。默认情况下,嵌入式服务器在端口8080上侦听HTTP请求。默认情况下,引入以下依赖项以使用Tomcat服务器。org.springframework.bootspring-boot-starter-web如果需要使用Undertow等其他服务器作为服务器,只需要以下依赖项。<依赖>org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-tomcatorg.springframework.bootspring-boot-starter-undertow复制代码artifactId>Servlets、Filters和listeners在使用嵌入式servlet容器时,您可以通过使用Springbean或扫描servlet组件来注册servlets、filters和servlet规范(例如HttpSessionListener)中的所有监听器。将Servlet、过滤器和侦听器注册为SpringBeans任何Springbean的Servlet、过滤器或*Listener实例都已向嵌入式容器注册。如果您想从应用程序中引用一个值,这会特别方便。配置期间的属性。默认情况下,如果上下文只包含一个servlet,它会映射到/。在多个servletbean的情况下,bean名称用作路径前缀。过滤映射到/*。如果基于约定的映射不够灵活,您可以使用ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean类进行完全控制。通常,将FilterBeans乱序放置是安全的。如果需要特定的顺序,过滤器应该用@Order注释,或者让它实现Ordered。您不能通过使用@Order注释来配置过滤器bean方法的顺序。如果您无法更改过滤器类以添加@Order或实现Ordered,则必须为过滤器定义一个FilterRegistrationBean并使用setOrder(int)方法设置已注册bean的顺序。避免配置按顺序读取请求主体的过滤器。注意:要查看应用程序中每个过滤器的顺序,请为Web日志组启用调试级别日志记录(logging.level.web=debug)。已注册过滤器的详细信息,包括它们的顺序和URL模式,将在启动时记录下来。Servlet上下文初始化嵌入式servlet容器不直接实现servlet3.0+javax.servlet.ServletContainerInitializer接口或Spring的org.springframework.web.WebApplicationInitializer接口。这样做是为了降低第三方库在战争中运行破坏SpringBoot应用程序的风险。如果您需要在SpringBoot应用程序中执行servlet上下文初始化,您应该注册一个实现org.springframework.boot.web.servlet.ServletContextInitializer接口的bean。单个onStartup方法提供对ServletContext的访问,如果需要,可以轻松地将其用作现有WebApplicationInitializer的适配器。扫描Servlet、过滤器和侦听器。使用嵌入式容器时,可以使用@ServletComponentScan启用自动注册使用@WebServlet、@WebFilter和@WebListener注释的类。容器的ServletWebServerApplicationContext位于底部,SpringBoot使用另一种ApplicationContext来支持嵌入式servlet容器。ServletWebServerApplicationContext是一种特殊类型的WebApplicationContext,它通过搜索单个ServletWebServerFactorybean来引导自己。通常TomcatServletWebServerFactory、JetttyServletWebServerFactory或UndertowServletWebServerFactory是自动配置的。您通常不需要知道这些实现类。大多数应用程序都是自动配置的,并创建适当的ApplicationContext和ServletWebServerFactory。在嵌入式容器设置中,ServletContext被设置为服务器启动的一部分,这发生在应用程序上下文初始化期间。因此,ApplicationContext中的bean不能用ServletContext可靠地初始化。解决这个问题的一种方法是将ApplicationContext作为bean的依赖项注入,并仅在需要时访问ServletContext。另一种方法是在服务器启动后使用回调函数。这个可以使用ApplicationListener来监听ApplicationStartedEvent,如下所示:publicclassCustomServletContextListenerimplementsApplicationListener{privateServletContextservletContext;@OverridepublicvoidonApplicationEvent(ApplicationStartedEventevent){ApplicationContextapplicationContext=event.getApplicationContext();this.servletContext=((WebApplicationContext)applicationContext).getServletContext();}}自定义servlet容器可以使用Spring环境属性来配置通用servlet容器设置。application.properties或application.yaml文件。常见的服务器设置包括:网络设置:监听传入HTTP请求的端口(server.port),绑定到server.address。地址等会话设置:会话是否持久化(server.servlet.session.persistent)、会话超时时间(server.servlet.session.timeout)、会话数据位置(server.servlet.session.store-dir)、以及会话cookie配置(服务器.servlet.session.cookie.*)。错误管理:错误页面的位置(server.error.path)等SSLHTTP压缩程序化自定义配置如果需要以程序化方式配置嵌入式servlet容器,可以注册一个实现了WebServerFactoryCustomizer接口的Springbean。WebServerFactoryCustomizer提供对ConfigurableServletWebServerFactory的访问,其中包括许多自定义设置方法。示例:@ComponentpublicclassMyWebServerFactoryCustomizerimplementsWebServerFactoryCustomizer{@Overridepublicvoidcustomize(ConfigurableServletWebServerFactoryserver){server.setPort(9000);}}TomcatServletWebServerFactory,JetttyServletWebServerFactory和UndertowServletWebServerFactory是ConfigurableServletWebServerFactory的子类,它们分别为Tomcat、Jetty和Undertow提供了额外的自定义设置方法。示例:@ComponentpublicclassMyTomcatWebServerFactoryCustomizerimplementsWebServerFactoryCustomizerconnector.setAsyncTimeout(Duration.ofSeconds(20).toMillis()));}}JSP限制当使用嵌入式servlet容器运行SpringBoot应用程序(打包为可执行存档)时,对JSP支持存在一些限制。对于Jetty和Tomcat,如果您使用war包装,它应该可以正常工作。使用java-jar启动时不支持jsp。Undertow不支持jsp。创建自定义error.jsp页面不会覆盖错误处理的默认视图。应该改用自定义错误页面。