一步一步学springboot (六)集成拦截器和过滤器及监听器

一、实现过滤器Filter

1.新建类(注意这个类上的注解@Component,这个注解不可以用,如果不用,就得在springboot的那个标有@SpringBootApplication的类上加上@ServletComponentScan,总之是让spring扫描并管理这个类)

@WebFilter(filterName="myFilter",urlPatterns="/*") 
//@Component
public class MyFilter implements Filter{
	@Override
	public void destroy() {
		System.out.println("过滤器销毁。。。。。。。");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("CustomFilter指定过滤器操作......");  
        //执行操作后必须doFilter  
        chain.doFilter(request, response);  
	}
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("CustomFilter初始化......");  
	}

}

二、监听器

注意这个类上的注解@Component,这个注解不可以用,如果不用,就得在springboot的那个标有@SpringBootApplication的类上加上@ServletComponentScan,总之是让spring扫描并管理这个类)

@WebListener
//@Component
public class MyListener implements ServletContextListener{
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("servletContext销毁......");
	}
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("servletContext初始化......");  
	}
}

三、拦截器

1.新建拦截器1

public class MyInterceptor1  implements HandlerInterceptor{
	@Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,  
            Exception exception) throws Exception {  
        System.out.println("3.整个请求结束之后被调用......CustomInterceptor1......");  
    }  
  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView view)  
            throws Exception {  
        System.out.println("2. 请求处理之后进行调用,但是在视图被渲染之前......CustomInterceptor1......");  
    }  
  
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {  
        System.out.println("1. 在请求处理之前进行调用......CustomInterceptor1......");  
        return true;  
    } 
	
}

2.新建拦截器2(多个拦截器可以组成拦截器链)

public class MyInterceptor2 implements HandlerInterceptor{
	@Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,  
            Exception exception) throws Exception {  
        System.out.println("3.整个请求结束之后被调用......CustomInterceptor2......");  
    }  
  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView view)  
            throws Exception {  
        System.out.println("2. 请求处理之后进行调用,但是在视图被渲染之前......CustomInterceptor2......");  
    }  
  
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {  
        System.out.println("1. 在请求处理之前进行调用......CustomInterceptor2......");  
        return true;  
    } 
}

3.新建一个拦截器注册类

(注意:@Configuration @Component两个注解选一个就可以,也是让spring管理这个类

//@Configuration
@Component
public class MyInterceptorConfig extends WebMvcConfigurerAdapter{

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//众多的拦截器组成了一个拦截器链  
        /** 
         * 主要方法说明: 
         * addPathPatterns 用于添加拦截规则 
         * excludePathPatterns 用户排除拦截 
         */  
        registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/*");  
        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/*");  
		super.addInterceptors(registry);
	}
	
}






猜你喜欢

转载自blog.csdn.net/u010509052/article/details/70666600