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>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注