Spring Boot 之web Filter --不支持排序的使用

来源:互联网 发布:安卓鼓机软件loopz 编辑:程序博客网 时间:2024/05/01 02:38

Spring Boot 之web Filter --不支持排序的使用


在severlet3.0以后的环境下,我们使用web Filter,可以利用注解WebFilter使用java配置的方式。
同样,spring boot环境一下依然可以使用:
package com.sdcuike.practice.web;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 javax.servlet.annotation.WebFilter;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.annotation.Order;import com.sdcuike.practice.config.CommonConfig;/** * FilterDemo1 *  * @author sdcuike *         <p> *         Created on 2017-02-10 *         <p> *         支持依赖注入 */@WebFilter("/*")public class FilterDemo1 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");    }}

  为了让spring boot扫描到注解的filter,我们还需要配置:

package com.sdcuike.practice.web;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Configuration;/** * web 组件配置 *  * @author sdcuike *         <p> *         Created on 2017-02-09 *         <p> *         web组件如Filter等注解配置,支持依赖注入,但spring的@Order注解不支持排序; * @WebFilter has no element to define the order of filter of execution. */@Configuration@ServletComponentScanpublic class WebComponentConfig {}

由于WebComponentConfig的包和filter所在的包同级目录(子目录也可以),注解ServletComponentScan默认
扫描与配置类WebComponentConfig同包及子包下面的filter,我们可以省去包名的书写。
 大家也看到了注解:@WebFilter has no element to define the order of filter of execution.但
我们使用filter的时候必须排序的情况下,就不能用这个方法了。

  有人说,我们可以利用注解:@Order如:
@WebFilter("/*")@Order(1)public class FilterDemo2 implements Filter {    private final Logger log = LoggerFactory.getLogger(getClass());
  或者实现spring 的排序接口:Ordered ,如:
package com.sdcuike.practice.web;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 javax.servlet.annotation.WebFilter;import org.springframework.core.Ordered;import com.sdcuike.practice.config.CommonConfig;import lombok.extern.slf4j.Slf4j;@WebFilter("/*")@Slf4jpublic class FilterDemo5 implements Filter, Ordered {    private final int    order = 66;    @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");    }    @Override    public int getOrder() {        return order;    }}

对不起,spring boot不支持。详见:https://github.com/spring-projects/spring-boot/issues/8276.

    由于兴趣,我修改了并扩展了spring boot的某些方法,可以支持,详情见以后序列博文,或直接去看源码。

本博客相关源码:https://github.com/sdcuike/spring-boot-practice/tree/master/src/main/java/com/sdcuike/practice/web



<spring-boot.version>1.5.1.RELEASE</spring-boot.version>
0 0