SpringBoot initial tutorial Servlet Filter Listener configuration details

  • 2020-10-31 21:44:45
  • OfStack

1. Introduction

As seen in previous articles, SpringBoot covers a lot of configuration, but often some configuration is done using the native Servlet, but in SpringBoot you do not need to configure ES6en.xml

Since it is possible to package in the form of 1 jar package, how to solve this situation? SpringBoot offers two solutions

2. Start fast

2.1 plan 1

Scheme 1 USES the native Servlet3.0 annotations for configuration. @WebServlet, @WebListener, and @WebFilter are provided in Servlet3.0 api
The annotations completely replace the configuration in ES26en.xml. Here is a simple configuration

IndexServlet


@WebServlet(name = "IndexServlet",urlPatterns = "/hello")
  public class IndexServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.getWriter().print("hello word");
      resp.getWriter().flush();
      resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      this.doGet(req, resp);
    }
  }

IndexListener


 @WebListener
  public class IndexListener implements ServletContextListener {
    private Log log = LogFactory.getLog(IndexListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
      log.info("IndexListener contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
  }

IndexFilter


@WebFilter(urlPatterns = "/*", filterName = "indexFilter")
  public class IndexFilter implements Filter {
    Log log = LogFactory.getLog(IndexFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
      log.info("init IndexFilter");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      log.info("doFilter IndexFilter");
      filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
  }

The above configuration has been completed, and a core annotation @ServletComponentScan needs to be configured. The specific configuration items are as follows, and the scanning path can be configured


@Target(ElementType.TYPE)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  @Import(ServletComponentScanRegistrar.class)
  public @interface ServletComponentScan {


    @AliasFor("basePackages")
    String[] value() default {};


    @AliasFor("value")
    String[] basePackages() default {};


    Class<?>[] basePackageClasses() default {};

  }

Just add the annotation to the entrance and start it


  @SpringBootApplication
  @ServletComponentScan
  public class AppApplication {

    public static void main(String[] args) throws Exception {
      SpringApplication.run(AppApplication.class, args);
    }

  }

2.2 plan 2

Scheme 2 is configured by SpringBoot in the way of bean. SpringBoot provides three types of BeanFilterRegistrationBean, ServletRegistrationBean and ServletListenerRegistrationBean corresponding to the configuration of the native Filter, Servlet and Listener respectively. The following three configurations and the way adopted in Scheme 1 can achieve the effect of unified 1


  @Bean
  public ServletRegistrationBean indexServletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet());
    registration.addUrlMappings("/hello");
    return registration;
  }

  @Bean
  public FilterRegistrationBean indexFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter());
    registration.addUrlPatterns("/");
    return registration;
  }
  @Bean
  public ServletListenerRegistrationBean servletListenerRegistrationBean(){
    ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
    servletListenerRegistrationBean.setListener(new IndexListener());
    return servletListenerRegistrationBean;
  }

3. Summary

There are differences in usage between the two scenarios, but there is no difference in the internal IMPLEMENTATION of SpringBoot, even with the es66EN3.0 annotations, which are scanned annotations
Convert to FilterRegistrationBean, ServletRegistrationBean and ServletListenerRegistrationBean of these 3 bean

4. The extension

Have you noticed when using SpringBoot that DispatcherServlet does not need to be configured when using SpringMvc, because it has been automatically configured, but if you want to add some initial configuration parameters, how to solve the problem is as follows:


@Bean
  public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
    registration.addUrlMappings("*.do");
    registration.addUrlMappings("*.json");
    return registration;
  }

You can dynamically add 1 initial parameter by injecting DispatcherServlet and wrapping 1 layer with ServletRegistrationBean

springboot-ES87en-ES88en-Listener_ES90en51.es91EN


Related articles: