springboot学习(七):Filter \Listener \Interceptor的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36553913/article/details/83512487

说明

之前学了在springboot项目中过滤器、监听器、拦截器的使用,在这里记录总结下。

正文

对于过滤器和监听器,在springboot中可以使用两种配置方式:
第一种,采用Servlet3.0的注解进行配置 @WebFilter @WebListener,使用此方式要在启动类上使用@ServletComponentScan注解
第二种,使用配置bean的方式,springboot提供了FilterRegistrationBean和ServletListenerRegistrationBean

Filter

实现Filter接口创建自定义过滤器
第一种

@WebFilter(urlPatterns = "/hello",filterName = "demofilter")
public class DemoFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("Remote Host: " + servletRequest.getRemoteHost());
        System.out.println("Remote Address: " + servletRequest.getLocalAddr());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

启动类添加@ServletComponentScan

@SpringBootApplication
@ServletComponentScan
public class FilterApplication {

	public static void main(String[] args) {
		SpringApplication.run(FilterApplication.class, args);
	}
}

第二种
不需要在DemoFilter类上添加注解,在启动类中创建bean

@SpringBootApplication
public class FilterApplication {

	public static void main(String[] args) {
		SpringApplication.run(FilterApplication.class, args);
	}

	@Bean
	public FilterRegistrationBean registrationBean(){
		FilterRegistrationBean filterbean = new FilterRegistrationBean();
		filterbean.setFilter(new DemoFilter());
		filterbean.setName("demofilter");
		filterbean.addUrlPatterns("/hello");
		return filterbean;
	}
}

Listener

实现ServletContextListener接口,创建项目启动监听类
第一种

@WebListener
public class DemoListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("DemoListener Context Initialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("DemoListener Context Destroyed");
    }
}

注意@ServletComponentScan一定不能忘

@SpringBootApplication
@ServletComponentScan
public class ListenerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ListenerApplication.class, args);
	}
}

第二种

@SpringBootApplication
public class ListenerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ListenerApplication.class, args);
	}

	@Bean
	public ServletListenerRegistrationBean<ServletContextListener> listenerRegistrationBean(){
		ServletListenerRegistrationBean listenerRegistrationBean= new ServletListenerRegistrationBean();
		listenerRegistrationBean.setListener(new DemoListener());
		return listenerRegistrationBean;
	}
}

Interceptor

对于拦截器,需要使用WebMvcConfigurerAdapter进行注册,继承此类重写addInterceptors方法,将自定义的拦截器进行注册。
实现HandlerInterceptor接口创建自定义拦截器

@Component
public class DemoInterceptor implements HandlerInterceptor {

    // This is used to perform operations before sending the request to the controller.
    // This method should return true to return the response to the client
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("Pre Handle method is Calling");
        return true;
    }

    // This is used to perform operations before sending the response to the client
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("Post Handle method is Calling");
    }

    // This is used to perform operations after completing the request and response
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("Request and Response is completed");
    }
}
@Component
public class DemoInterceptorConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private DemoInterceptor demoInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor).addPathPatterns("/hello");
    }
}

源码地址:
https://github.com/Edenwds/springboot_study/tree/master/filter
https://github.com/Edenwds/springboot_study/tree/master/listener
https://github.com/Edenwds/springboot_study/tree/master/interceptor

参考资料:
https://www.tutorialspoint.com/spring_boot/index.htm
https://blog.csdn.net/liu455042806/article/details/79875999?utm_source=blogxgwz0
https://blog.csdn.net/king_is_everyone/article/details/53116744?utm_source=blogxgwz5

扫描二维码关注公众号,回复: 4133014 查看本文章

猜你喜欢

转载自blog.csdn.net/sinat_36553913/article/details/83512487
今日推荐