过滤器和拦截器的执行链路
过滤器和拦截器的异同
- 过滤器(Filter):当你有一堆东西的时候,你只希望选择符合你要求的某一些东西。定义这些 要求的工具,就是过滤器。
- 拦截器(Interceptor):在一个流程正在进行的时候,你希望干预它的进展,甚至终止它进行, 这是拦截器做的事情。
- 相同点:都是aop编程思想的体现,可以在程序执行前后做一些操作
- 不同点:过滤器依赖于servlet容器,拦截器不依赖 过滤器的执行由Servlet容器回调完成,而拦截器通常通过动态代理的方式来执行。 触发时机不一样,过滤器是在请求进入Tomcat容器后,而进入servlet前进行预处理的;拦 截器是在进入servlet之后,而进入controller之前处理的。 拦截器可以获取IOC容器中的各个bean,而过滤器就不行,拦截器归Spring管理。
过滤器一
package com.duing.filter;
import javax.servlet.*;
import java.io.IOException;
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(".............CustomFilter init............");
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain)
throws IOException, ServletException {
System.out.println(".............CustomFilter doFilter............");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println(".............CustomFilter destroy............");
}
}
过滤器二
package com.duing.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "customFilter2",urlPatterns = {"/*"})
public class CustomFilter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(".............CustomFilter2 init............");
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain)
throws IOException, ServletException {
System.out.println(".............CustomFilter2 doFilter............");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println(".............CustomFilter2 destroy............");
}
}
过滤器配置类
FilterConfig
import com.duing.filter.CustomFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* web.xml
* <filter>
* <filter-name>struts2</filter-name>
* <filter-class>com.duing.filter.CustomFilter</filter-class>
* </filter>
*
* <filter-mapping>
* <filter-name>struts2</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
*/
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean<CustomFilter> filterRegistrationBean(){
FilterRegistrationBean<CustomFilter> filterFilterRegistrationBean=
new FilterRegistrationBean<>();
filterFilterRegistrationBean.setFilter(new CustomFilter());
filterFilterRegistrationBean.addUrlPatterns("/*");
// filterFilterRegistrationBean.setOrder(0); //决定注册的优先级
return filterFilterRegistrationBean;
}
}
拦截器
package com.duing.interceptor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Service
public class CustomInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
System.out.println("...........CustomInterceptor prehandle...........");
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
System.out.println("...........CustomInterceptor postHandle...........");
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
System.out.println("...........CustomInterceptor afterCompletion...........");
}
}
拦截器配置类
import com.duing.interceptor.CustomInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer{
@Autowired
private CustomInterceptor customInterceptor;
/**
* 注册自定义的拦截器,并且定义拦截规则
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(customInterceptor).addPathPatterns("/**");
}
}
controller
package com.duing.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServletController {
@RequestMapping("/servlet")
public String servlet(){
System.out.println("ServletController servlet........");
return "Hello Servlet";
}
}
springboot启动类
package com.duing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //扫描@WebFilter(filterName = "customFilter2",urlPatterns = {"/*"})过滤器注解
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
网友评论