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: 80
servlet:
context-path: /aa
# tomcat 相关配置
tomcat:
uri-encoding: UTF-8
(2)编写一个 EmbeddedServletContainerCustomizer,它是嵌入式servlet容器的定制器,可以用它来修改servlet的配置.
EmbeddedServletContainerCustomizer 被 WebServerFactoryCustomizer 代替,因此要使用下面这种方式来实现:
@Configuration
public class WebConfig {
/**
* 修改 Servlet 容器配置
* @return
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryWebServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public 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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected 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
*/
@Bean
public ServletRegistrationBean myServlet(){
return new ServletRegistrationBean(new MyServlet(),"/myServlet"); // 参数(servlet对象, 设置映射的路径)
}
}
2、FilterRegistrationBean:注册Filter
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter 过滤器执行了...");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
//设置要拦截的请求路径
filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet", "/index"));
return filterRegistrationBean;
}
3、ServletListenerRegistrationBean:注册Listener
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("自己定义的MyServlet...");
}
}
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new
ServletListenerRegistrationBean(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 ~