**
在SpringBoot中过滤器、监听器、拦截器简介及其的应用**
**
过滤器:依赖于servlet容器,对几乎所有的请求起作用,在容器启动时初始化调用init方法,以后每个请求都调用doFilter(),主要的用途是过滤字符编码、做一些业务逻辑判断等。其工作原理是,它是随你的web应用启动而启动的,只初始化一次,以后就可以拦截相关请求,只有当你的web应用停止或重新部署加载的时候才销毁。
过滤器在SpringBoot中的应用:
方法一:
> //注册器名称为customFilter,拦截的url为所有
> @WebFilter(filterName="customFilter",urlPatterns={"/*"})
public class CustomFilter implements Filter{
> @Override
> public void init(FilterConfig filterConfig) throws ServletException {
> log.info("filter 初始化");
> }
> @Override
> public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
> throws IOException, ServletException {
> // TODO Auto-generated method stub
> log.info("doFilter 请求处理");
> //对request、response进行一些预处理
> // 比如设置请求编码
> //TODO 进行业务逻辑
> //放行
> chain.doFilter(request, response);
> }
> @Override
> public void destroy() {
log.info("filter 销毁");
}
然后在启动类加入@ServletComponentScan注解即可。
@SpringBootApplication
@ServletComponentScan
public class Chapter7Application {
public static void main(String[] args) {
SpringApplication.run(Chapter7Application.class, args);
log.info("chapter7 服务启动");
}
}
方法二:启动类中利用@bean注册FilterRegistrationBean
> @Bean
public FilterRegistrationBean filterRegistrationBean() {
> FilterRegistrationBean registration = new FilterRegistrationBean();
> //当过滤器有注入其他bean类时,可直接通过@bean(注解bean)的方式进行实体类过滤器,这样不可自动注入过滤器使用的其他bean类。
> //当然,若无其他bean需要获取时,可直接new CustomFilter(),也可使用getBean的方式。
> registration.setFilter(customFilter()); //过滤器名称
> registration.setName("customFilter"); //拦截路径
> registration.addUrlPatterns("/*"); //设置顺序
> registration.setOrder(10);
return registration;
}
@Bean public
> Filter customFilter() {
return new CustomFilter();
}
监听器:Listener是servlet规范中定义的一种特殊类。用于监听servletContext、HttpSession和servletRequest等域对象的创建和销毁事件。监听域对象的属性发生修改的事件。用于在事件发生前、发生后做一些必要的处理。一般是获取在线人数等业务需求。
监听器在SpringBoot中的应用:
(和创建过滤器一样,在启动类中加入@ServletComponentScan进行自动注册即可。)**
@WebListener
public class Customlister implements
> ServletRequestListener{
> @Override
> public void requestDestroyed(ServletRequestEvent sre) {
> log.info("监听器:销毁");
> }
> @Override
> public void requestInitialized(ServletRequestEvent sre) {
> log.info("监听器:初始化");
> }
拦截器:是在面向切面编程的就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法,比如动态代理就是拦截器的简单实现.spring的aop中的前置通知和后置通知。
拦截器在SpringBoot中的应用:
public class CustomHandlerInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
log.info("preHandle:请求前调用");
//返回 false 则请求中断
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
log.info("postHandle:请求后调用");
}
@Override
Public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("afterCompletion:请求调用完成后回调方法,即在视图渲染完成后回调")
}
通过继承WebMvcConfigurerAdapter注册拦截器
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册拦截器 拦截规则
//多个拦截器时 以此添加 执行顺序按添加顺序
registry.addInterceptor(getHandlerInterceptor()).addPathPatterns("/*");
}
@Bean
public static HandlerInterceptor getHandlerInterceptor() {
return new CustomHandlerInterceptor();
}
}