SpringBoot配置Tomcat和Servlet三大组件

目录结构

  • 一,SpringBoot配置Tomcat容器
    • 1.使用SpringBoot配置文件配置Tomcat
    • 2.使用Java代码配置Tomcat
  • 二,SpringBoot配置Servlet三大组件
    • 1.Servlet
    • 2.Filter
    • 3.Listener

SpringBoot版本说明:

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.5.RELEASEversion>
        <relativePath/> 
    parent>

一,SpringBoot配置Tomcat容器

1.使用SpringBoot配置文件配置Tomcat

在SpringBoot的配置文件(application.properties,application.yaml)修改配置属性即可。

server相关配置:
SpringBoot配置Tomcat和Servlet三大组件_第1张图片
tomcat相关配置:
SpringBoot配置Tomcat和Servlet三大组件_第2张图片
Server和Tomcat配置相关联配置源码:

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

   /**
    * Server HTTP port.
    */
   private Integer port;

   /**
    * Network address to which the server should bind.
    */
   private InetAddress address;

   @NestedConfigurationProperty
   private final ErrorProperties error = new ErrorProperties();

   /**
    * Strategy for handling X-Forwarded-* headers.
    */
   private ForwardHeadersStrategy forwardHeadersStrategy;

   /**
    * Value to use for the Server response header (if empty, no header is sent).
    */
   private String serverHeader;

   /**
    * Maximum size of the HTTP message header.
    */
   private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8);

   /**
    * Type of shutdown that the server will support.
    */
   private Shutdown shutdown = Shutdown.IMMEDIATE;

   @NestedConfigurationProperty
   private Ssl ssl;

   @NestedConfigurationProperty
   private final Compression compression = new Compression();

   @NestedConfigurationProperty
   private final Http2 http2 = new Http2();

   private final Servlet servlet = new Servlet();

   private final Tomcat tomcat = new Tomcat();

   private final Jetty jetty = new Jetty();

   private final Netty netty = new Netty();

   private final Undertow undertow = new Undertow();
}

2.使用Java代码配置Tomcat

该配置使用的SpringBoot版本为2.3.5.RELEASE,版本不同,配置可能有所差异。

@Configuration
public class WebConfig  {

    /*
    * 基于Java代码配置Servlet容器
    * */
    @Bean
    public ServletWebServerFactoryCustomizer serverConfig(){
        ServerProperties serverProperties = new ServerProperties();
        // 使用ServerProperties配置相关属性
        serverProperties.setPort(8083);
        serverProperties.getServlet().setContextPath("/crud");

		// WebServer定制器
        ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer = new ServletWebServerFactoryCustomizer(serverProperties);
        return servletWebServerFactoryCustomizer;
    }
}

二,SpringBoot配置Servlet三大组件

1.Servlet

Servlet:

public class MyServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("serlvet请求响应。。。");
        req.getRequestDispatcher("/result.html").forward(req,resp);
    }
}

将该Servlet加入到配置类:

@Configuration
public class WebConfig  {
    /*
    * SpringBoot配置Servlet
    * */
    @Bean
    public ServletRegistrationBean<MyServlet> registrationServlet(){
        ServletRegistrationBean<MyServlet> servletRegistrationBean = new ServletRegistrationBean<>(new MyServlet());
        servletRegistrationBean.setUrlMappings(Arrays.asList("/servlet"));
        return servletRegistrationBean;
    }
}

2.Filter

Filter:

public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter拦截请求。。。");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

将该Filter加入到配置类:

@Configuration
public class WebConfig  {
   /*
    * SpringBoot配置Filter
    * */
    @Bean
    public FilterRegistrationBean<MyFilter> registrationFilter(){
        FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}

3.Listener

Listener:

public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Servlet容器注册。。。");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Servlet容器销毁。。。");
    }
}

将该Listener加入配置类:

@Configuration
public class WebConfig  {
    /*
    * SpringBoot配置Listener
    * */
    @Bean
    public ServletListenerRegistrationBean<MyListener> registrationListener(){
        ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return servletListenerRegistrationBean;
    }
}

该配置类最终运行结果:

SpringBoot配置Tomcat和Servlet三大组件_第3张图片

你可能感兴趣的:(tomcat,spring,boot,servlet,java)