Close

Spring Boot - Registering Servlet Components as Spring Beans

[Last Updated: Mar 30, 2017]

We saw in the last tutorial that Servlet 3.0 annotation can be scanned by using @ServletComponentScan. Sometimes, however, that will not be possible to register Servlet Components by using Servlet 3.0 annotations and we would instead like to register them to the container programmatically. For example a library Servlet (or any component) is needed to be registered but we cannot modify the source code to add @WebServlet annotation.

Rather than working directly with servlet API like ServletContext#addServlet, Spring provides following classes to register servlet components as beans.

By registering above beans with Spring context, will automatically register the underlying Servlet component to the Servlet Container. The Servlet component as a bean will also have all advantages which a Spring bean does, for example we can inject configuration or dependencies into them.


Example

We are going to modify our last tutorial example to demonstrate how to use above registration bean classes.

A Servlet component

public class MyServlet extends HttpServlet {

  @Override
  protected void doGet (HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

      PrintWriter writer = resp.getWriter();
      writer.println("response from servlet ");
  }
}

Registering Servlet using ServletRegistrationBean

@SpringBootApplication
public class Main {
    .............
  @Bean
  ServletRegistrationBean myServletRegistration () {
      ServletRegistrationBean srb = new ServletRegistrationBean();
      srb.setServlet(new MyServlet());
      srb.setUrlMappings(Arrays.asList("/path2/*"));
      return srb;
  }
    .............
}


A Filter component

public class MyFilter implements Filter {
  
  @Override
  public void init (FilterConfig filterConfig) throws ServletException {
  }

  @Override
  public void doFilter (ServletRequest request,
                        ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
      String url = request instanceof HttpServletRequest ?
                ((HttpServletRequest) request).getRequestURL().toString() : "N/A";
      System.out.println("from filter, processing url: " + url);
      chain.doFilter(request, response);
  }

  @Override
  public void destroy () {
  }
}

Registering Filter using FilterRegistrationBean

@SpringBootApplication
public class Main {
    .............
  @Bean
  FilterRegistrationBean myFilterRegistration () {
      FilterRegistrationBean frb = new FilterRegistrationBean();
      frb.setFilter(new MyFilter());
      frb.setUrlPatterns(Arrays.asList("/*"));
      return frb;
  }
    .............
}


A Filter component

public class MyServletListener implements ServletContextListener {
  
  @Override
  public void contextInitialized (ServletContextEvent sce) {
      System.out.println("from ServletContextListener: " +
                                   " context initialized");
  }

  @Override
  public void contextDestroyed (ServletContextEvent sce) {
  }
}

Registering Web Listener using ServletListenerRegistrationBean

@SpringBootApplication
public class Main {
    .............
  @Bean
  ServletListenerRegistrationBean<ServletContextListener> myServletListener () {
      ServletListenerRegistrationBean<ServletContextListener> srb =
                new ServletListenerRegistrationBean<>();
      srb.setListener(new MyServletListener());
      return srb;
  }
}

A Spring Controller

@Controller
public class MyController {

  @RequestMapping("/*")
  @ResponseBody
  public String handler () {
      return "response form spring controller method";
  }
}

The output of this example is exactly same as the last example.


Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.4.4.RELEASE
    Corresponding Spring Version 4.3.6.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • JDK 1.8
  • Maven 3.3.9

Servlet Components Beans Registration Select All Download
  • servlet-component-beans
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Main.java

    See Also