zoukankan      html  css  js  c++  java
  • Spring boot 注册Filter , Listener, Servlet

    1: ServletRegistrationBean   Servlet

    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return registrationBean;
    }

    或者通过@WebServlet注解也可以.

    2:FilterRegistrationBean  过滤器

    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
      //过滤的请求路径 registrationBean.setUrlPatterns(Arrays.asList(
    "/hello","/myServlet")); return registrationBean; }
    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 {
            System.out.println("MyFilter process...");
            chain.doFilter(request,response);
        }
        @Override
        public void destroy() {
        }
    }

    3:ServletListenerRegistrationBean 监听器

    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

    myListener 实现 ServletContextListener即可

  • 相关阅读:
    CentOS+nginx+uwsgi+Python 多站点环境搭建
    nginx实现负载均衡
    高性能Mysql主从架构的复制原理及配置详解
    centos 安装php
    Java知识总结-6
    Java知识总结-5
    Java知识总结-4
    Java知识总结-3
    Java知识总结-2
    java知识总结-1
  • 原文地址:https://www.cnblogs.com/eason-d/p/9315449.html
Copyright © 2011-2022 走看看