如果在我们的 Spring Boot 项目使用到了 Servlet、Filter、Listener,可以使用如下三种方式配置。

自定义 Servlet、Filter、Listener

  1. /**
  2. * 自定义 servlet
  3. */
  4. public class CustomServlet extends HttpServlet {
  5. private static final long serialVersionUID = 1L;
  6. @Override
  7. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  8. throws ServletException, IOException {
  9. System.out.println("servlet get method");
  10. doPost(request, response);
  11. }
  12. @Override
  13. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  14. throws ServletException, IOException {
  15. System.out.println("servlet post method");
  16. response.getWriter().write("hello world");
  17. }
  18. }
  1. /**
  2. * 自定义 filter
  3. */
  4. public class CustomFilter implements Filter {
  5. @Override
  6. public void init(FilterConfig filterConfig) throws ServletException {
  7. System.out.println("init filter");
  8. }
  9. @Override
  10. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  11. throws IOException, ServletException {
  12. System.out.println("do filter");
  13. chain.doFilter(request, response);
  14. }
  15. @Override
  16. public void destroy() {
  17. System.out.println("destroy filter");
  18. }
  19. }
  1. /**
  2. * 自定义 listener
  3. */
  4. public class CustomListener implements ServletContextListener {
  5. @Override
  6. public void contextInitialized(ServletContextEvent sce) {
  7. System.out.println("contextInitialized");
  8. }
  9. @Override
  10. public void contextDestroyed(ServletContextEvent sce) {
  11. System.out.println("contextDestroyed");
  12. }
  13. }

方式一:注册 Bean

创建自定义的 Servlet、Filter、Listener 类,然后通过在 Spring Boot 入口启动类中注册 ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 的方式使用。

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. @Bean
  4. public ServletRegistrationBean servletRegistrationBean() {
  5. return new ServletRegistrationBean(new CustomServlet(), "/test");
  6. }
  7. @Bean
  8. public FilterRegistrationBean filterRegistrationBean() {
  9. return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());
  10. }
  11. @Bean
  12. public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
  13. return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
  14. }
  15. public static void main(String[] args) {
  16. SpringApplication.run(DemoApplication.class, args);
  17. }
  18. }

方式二:实现 ServletContextInitializer 接口

通过实现 ServletContextInitializer 接口,实现其中的 onStartup 方法。

  1. @SpringBootApplication
  2. public class DemoApplication implements ServletContextInitializer {
  3. public static void main(String[] args) {
  4. SpringApplication.run(DemoApplication.class, args);
  5. }
  6. @Override
  7. public void onStartup(ServletContext servletContext) throws ServletException {
  8. servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/test");
  9. servletContext.addFilter("customFilter", new CustomFilter())
  10. .addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet");
  11. servletContext.addListener(new CustomListener());
  12. }
  13. }

方法三:通过注解

在 SpringBootApplication 上使用 @ServletComponentScan 注解后,直接通过@WebServlet、@WebFilter、@WebListener 注解自动注册。

  1. @SpringBootApplication
  2. @ServletComponentScan
  3. public class DemoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }
  1. /**
  2. * 自定义 servlet
  3. */
  4. @WebServlet(name = "customServlet", urlPatterns = "/test")
  5. public class CustomServlet extends HttpServlet {
  6. /**
  7. * 自定义 filter
  8. */
  9. @WebFilter(filterName = "customFilter", urlPatterns = "/*")
  10. public class CustomFilter implements Filter {
  11. /**
  12. * 自定义 listener
  13. */
  14. @WebListener
  15. public class CustomListener implements ServletContextListener {