SpringBoot 与 Servlet 整合的两种方式,一种是 Servlet 3.0 原生的注解方式,一种是采用 Spring 注册的方式

1. Spring 注册方式

1.1 组件声明

声明过滤器、监听器和 Servlet

  1. /**
  2. * @description : 自定义过滤器
  3. */
  4. public class CustomFilter implements Filter {
  5. @Override
  6. public void init(FilterConfig filterConfig) throws ServletException {
  7. }
  8. @Override
  9. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  10. request.setAttribute("filterParam","我是 filter 传递的参数");
  11. chain.doFilter(request,response);
  12. response.getWriter().append(" CustomFilter ");
  13. }
  14. @Override
  15. public void destroy() {
  16. }
  17. }
  1. /**
  2. * @description : 自定义监听器
  3. */
  4. public class CustomListen implements ServletContextListener {
  5. //Web 应用程序初始化过程正在启动的通知
  6. @Override
  7. public void contextInitialized(ServletContextEvent sce) {
  8. System.out.println("容器初始化启动");
  9. }
  10. /* 通知 servlet 上下文即将关闭
  11. * 这个地方如果我们使用的是 spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
  12. */
  13. @Override
  14. public void contextDestroyed(ServletContextEvent sce) {
  15. System.out.println("容器即将销毁");
  16. }
  17. }
  1. /**
  2. * @description : 自定义 servlet
  3. */
  4. public class CustomServlet extends HttpServlet {
  5. @Override
  6. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  7. System.out.println("doGet 执行:" + req.getAttribute("filterParam"));
  8. resp.getWriter().append("CustomServlet");
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. doGet(req, resp);
  13. }
  14. }

1.2 组件注册

  1. @Configuration
  2. public class ServletConfig {
  3. @Bean
  4. public ServletRegistrationBean registrationBean() {
  5. return new ServletRegistrationBean<HttpServlet>(new CustomServlet(), "/servlet");
  6. }
  7. @Bean
  8. public FilterRegistrationBean filterRegistrationBean() {
  9. FilterRegistrationBean bean = new FilterRegistrationBean<Filter>();
  10. bean.setFilter(new CustomFilter());
  11. bean.addUrlPatterns("/servlet");
  12. return bean;
  13. }
  14. @Bean
  15. public ServletListenerRegistrationBean listenerRegistrationBean() {
  16. return new ServletListenerRegistrationBean<ServletContextListener>(new CustomListen());
  17. }
  18. }

2. 原生注解方式

2.1 组件声明

新建过滤器、监听器和 servlet,分别使用 @WebFilter、@WebListener、@WebServlet 注解进行声明:

  1. /**
  2. * @description : 自定义过滤器
  3. */
  4. @WebFilter(urlPatterns = "/servletAnn")
  5. public class CustomFilterAnnotation implements Filter {
  6. @Override
  7. public void init(FilterConfig filterConfig) throws ServletException {
  8. }
  9. @Override
  10. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  11. chain.doFilter(request,response);
  12. response.getWriter().append(" CustomFilter Annotation");
  13. }
  14. @Override
  15. public void destroy() {
  16. }
  17. }
  1. /**
  2. * @description :自定义监听器
  3. */
  4. @WebListener
  5. public class CustomListenAnnotation implements ServletContextListener {
  6. //Web 应用程序初始化过程正在启动的通知
  7. @Override
  8. public void contextInitialized(ServletContextEvent sce) {
  9. System.out.println("容器初始化启动 Annotation");
  10. }
  11. /* 通知 servlet 上下文即将关闭
  12. * 这个地方如果我们使用的是 spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
  13. */
  14. @Override
  15. public void contextDestroyed(ServletContextEvent sce) {
  16. System.out.println("容器即将销毁 Annotation");
  17. }
  18. }
  1. /**
  2. * @description : 自定义 servlet
  3. */
  4. @WebServlet(urlPatterns = "/servletAnn")
  5. public class CustomServletAnnotation extends HttpServlet {
  6. @Override
  7. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  8. resp.getWriter().append("CustomServlet Annotation");
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. doGet(req, resp);
  13. }
  14. }

2.2 组件生效

想要让使用 Servlet 3.0 原生注解方式声明的组件生效,有以下两种方式:

  1. 如果是内置容器,需要在启动类上添加 @ServletComponentScan ,指定扫描的包目录;
  2. 如果是外置容器,不需要进行任何配置,依靠容器内建的 Discovery 机制自动发现,需要说明的是这里的容器必须支持 Servlet 3.0( Tomcat 从 7.0 版本开始支持 Servlet3.0)。