Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)
Posted 木西 - Muxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)相关的知识,希望对你有一定的参考价值。
Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用
Spring 提供了FilterRegistrationBean类,此类提供setOrder方法,可以为filter设置排序值,
让spring在注册web filter之前排序后再依次注册。
写一个普通的filter:
package com.sdcuike.practice.web2; import java.io.IOException; import javax.annotation.Resource; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sdcuike.practice.config.CommonConfig; public class FilterDemo3 implements Filter { private final Logger log = LoggerFactory.getLogger(getClass()); @Resource private CommonConfig commonConfig; @Override public void destroy() { log.info("" + getClass() + " destroy"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { log.info("" + getClass() + " doFilter " + commonConfig); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { log.info("" + getClass() + " init"); } }
配置如下:
package com.sdcuike.practice.web2; import javax.servlet.Filter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.sdcuike.spring.extend.web.MvcConfigurerAdapter; /** * web 组件配置 * * @author sdcuike * <p> * Created on 2017-02-10 * <p> * 自定义注入,并支持依赖注入,组件排序 */ @Configuration public class WebComponent2Config { @Bean public FilterRegistrationBean filterDemo3Registration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(filterDemo3()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("filterDemo3"); registration.setOrder(6); return registration; } @Bean public FilterRegistrationBean filterDemo4Registration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(filterDemo4()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("filterDemo4"); registration.setOrder(7); return registration; } @Bean public Filter filterDemo3() { return new FilterDemo3(); } @Bean public Filter filterDemo4() { return new FilterDemo4(); } }
利用这种方式,我们可以对filter排序,可自行测试,源码:
————————————————
版权声明:本文为CSDN博主「A_Beaver」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/doctor_who2004/article/details/56055505
以上是关于Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 系列之四:spring boot 整合JPA
Spring Boot参考教程Spring Boot配置使用之配置文件用法