1. 程式人生 > >一步一步學springboot (六)整合攔截器和過濾器及監聽器

一步一步學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);
	}
	
}