国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

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

2019-11-08 01:33:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

在severlet3.0以后的環(huán)境下,我們使用web Filter,可以利用注解WebFilter使用java配置的方式。同樣,spring boot環(huán)境一下依然可以使用:
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> *         支持依賴(lài)注入 */@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等注解配置,支持依賴(lài)注入,但spring的@Order注解不支持排序; * @WebFilter has no element to define the order of filter of execution. */@Configuration@ServletComponentScanpublic class WebComponentConfig {}由于WebComponentConfig的包和filter所在的包同級(jí)目錄(子目錄也可以),注解ServletComponentScan默認(rèn)掃描與配置類(lèi)WebComponentConfig同包及子包下面的filter,我們可以省去包名的書(shū)寫(xiě)。 大家也看到了注解:@WebFilter has no element to define the order of filter of execution.但我們使用filter的時(shí)候必須排序的情況下,就不能用這個(gè)方法了。  有人說(shuō),我們可以利用注解:@Order如:
@WebFilter("/*")@Order(1)public class FilterDemo2 implements Filter {    private final Logger log = LoggerFactory.getLogger(getClass());  或者實(shí)現(xiàn)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;    }}對(duì)不起,spring boot不支持。詳見(jiàn):https://github.com/spring-projects/spring-boot/issues/8276.    由于興趣,我修改了并擴(kuò)展了spring boot的某些方法,可以支持,詳情見(jiàn)以后序列博文,或直接去看源碼。本博客相關(guān)源碼: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>
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石家庄市| 蓝田县| 鄢陵县| 民县| 且末县| 石景山区| 开封县| 临武县| 蒙城县| 汉源县| 康保县| 龙泉市| 莆田市| 长春市| 法库县| 拉萨市| 广汉市| 三原县| 神农架林区| 门头沟区| 罗甸县| 黑龙江省| 延川县| 晋州市| 松溪县| 芦山县| 都江堰市| 宝鸡市| 邛崃市| 卓资县| 谷城县| 旬邑县| 鄢陵县| 临西县| 桃园市| 南溪县| 定兴县| 饶阳县| 七台河市| 姚安县| 兴和县|