springboot掃描自定義的servlet和filter代碼詳解
這幾天使用spring boot編寫公司一個(gè)應(yīng)用,在編寫了一個(gè)filter,用于指定編碼的filter,如下:
/** * Created by xiaxuan on 16/11/1. */ @WebFilter(urlPatterns = "/*",filterName="CharacterEncodeFilter", initParams={ @WebInitParam(name="encoding",value="UTF-8"), @WebInitParam(name = "forceEncoding", value = "true") }) @Singleton public class CharacterEncodingFilter implements Filter { private String encoding = "UTF-8"; private boolean forceEncoding = true; @Override public void init(FilterConfig filterConfig) throws ServletException { this.encoding = filterConfig.getInitParameter("encoding"); String force = filterConfig.getInitParameter("forceEncoding"); this.forceEncoding = (force == null) || Boolean.valueOf(force); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (this.forceEncoding || request.getCharacterEncoding() == null) { request.setCharacterEncoding(this.encoding); response.setCharacterEncoding(this.encoding); } chain.doFilter(request, response); } @Override public void destroy() { } public void setEncoding(String encoding) { this.encoding = encoding; } public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } }
但是在實(shí)際使用的時(shí)候,卻是完全沒有起作用,后來查看了一下springboot的官方文檔,filter和servlet、listener之類的需要單獨(dú)進(jìn)行注冊(cè)才能使用,但是spring boot里面提供了一個(gè)注解來替代,為@ServletComponentScan,這個(gè)注解直接加在對(duì)應(yīng)的Application啟動(dòng)類上即可,如下:
@SpringBootApplication @ServletComponentScan @ComponentScan public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } }
這樣編寫完之后,如果對(duì)應(yīng)的filter是在自己當(dāng)前模塊下的某個(gè)package中的時(shí)候是可以起作用的,但是如果本身項(xiàng)目中有多個(gè)模塊的時(shí)候,如果filter在一個(gè)類似與core下的package中,這樣注解加上去并沒有多大用處,最后會(huì)發(fā)現(xiàn)這個(gè)filter仍然沒有起作用。
我自己編寫的應(yīng)用有兩個(gè),最開始的做法是把filter從core包中拆出來,然后在兩個(gè)模塊中各自添加一個(gè),但是這樣未免有些代碼冗余,并且實(shí)現(xiàn)方式并不優(yōu)雅,然后我查看了下@ServletComponentScan的源碼,里面確實(shí)是有更好的解決方法。
@ServletComponentScan的源碼如下:
/** * Enables scanning for Servlet components ({@link WebFilter filters}, {@link WebServlet * servlets}, and {@link WebListener listeners}). Scanning is only performed when using an * embedded Servlet container. * <p> * Typically, one of {@code value}, {@code basePackages}, or {@code basePackageClasses} * should be specified to control the packages to be scanned for components. In their * absence, scanning will be performed from the package of the class with the annotation. * * @author Andy Wilkinson * @since 1.3.0 * @see WebServlet * @see WebFilter * @see WebListener */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(ServletComponentScanRegistrar.class) public @interface ServletComponentScan { /** * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation * declarations e.g.: {@code @ServletComponentScan("org.my.pkg")} instead of * {@code @ServletComponentScan(basePackages="org.my.pkg")}. * @return the base packages to scan */ @AliasFor("basePackages") String[] value() default {}; /** * Base packages to scan for annotated servlet components. {@link #value()} is an * alias for (and mutually exclusive with) this attribute. * <p> * Use {@link #basePackageClasses()} for a type-safe alternative to String-based * package names. * @return the base packages to scan */ @AliasFor("value") String[] basePackages() default {}; /** * Type-safe alternative to {@link #basePackages()} for specifying the packages to * scan for annotated servlet components. The package of each class specified will be * scanned. * @return classes from the base packages to scan */ Class<?>[] basePackageClasses() default {}; }
這里有一個(gè)value()屬性,上面的注解默認(rèn)為basePackage,那么在掃描的時(shí)候就只掃描當(dāng)前模塊下面的包,其他不掃描,如果要連同其他模塊一起掃描的話,給這個(gè)屬性加上值即可,如下:
@ServletComponentScan(value = "cn.com")
如上,自定義的filter和servlet就可以正常起作用。
總結(jié)
以上就是本文關(guān)于springboot掃描自定義的servlet和filter代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:淺談Java注解和動(dòng)態(tài)代理 、Java之Spring注解配置bean實(shí)例代碼解析、淺談Springboot之于Spring的優(yōu)勢(shì)等。有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家。同時(shí)希望朋友們對(duì)腳本之家網(wǎng)站多多支持!
- SpringBoot集成WebServlet出現(xiàn)自定義servlet請(qǐng)求失敗的問題解決方案
- SpringBoot里使用Servlet進(jìn)行請(qǐng)求的實(shí)現(xiàn)示例
- Springboot注入成員變量HttpServletRequest的原理分析
- SpringBoot3.1.2 引入Swagger報(bào)錯(cuò)Type javax.servlet.http.HttpServletRequest not present解決辦法
- 解決IDEA啟動(dòng)springboot項(xiàng)目報(bào)錯(cuò)java.lang.ClassNotFoundException:?javax.servlet.ServletContext
- SpringBoot獲取HttpServletRequest的3種方式總結(jié)
- Springboot如何添加server.servlet.context-path相關(guān)使用
- SpringBoot項(xiàng)目找不到j(luò)avax.servlet.Filter的問題及解決
- SpringBoot如何切換成其它的嵌入式Servlet容器(Jetty和Undertow)
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):稀疏數(shù)組
今天帶大家了解一下Java稀疏數(shù)組的相關(guān)知識(shí),文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-08-08java開發(fā)中基于JDBC連接數(shù)據(jù)庫實(shí)例總結(jié)
這篇文章主要介紹了java開發(fā)中基于JDBC連接數(shù)據(jù)庫的方法,以實(shí)例形式較為詳細(xì)的總結(jié)分析了Java使用JDBC的具體步驟與注意事項(xiàng),并附帶了一個(gè)完整實(shí)例加以說明,需要的朋友可以參考下2015-11-11Spring SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析
這篇文章主要介紹了SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析,還是非常不錯(cuò)的,在這里分享給大家,需要的朋友可以參考下。2017-09-09Spring中Transactional注解使用的心得(推薦)
這篇文章主要介紹了Spring中Transactional注解使用的心得,事務(wù)是用來控制數(shù)據(jù)的ACID特性的,用于保證數(shù)據(jù)的正確性和完整性,需要的朋友可以參考下2022-10-10SpringBoot整合Redis實(shí)現(xiàn)緩存分頁數(shù)據(jù)查詢功能
類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎,答案肯定不是,本文我們就通過一個(gè)案例實(shí)操一下,首頁熱點(diǎn)數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學(xué)可以參考一下2023-06-06IDEA在創(chuàng)建包時(shí)如何把包分開實(shí)現(xiàn)自動(dòng)分層(方法詳解)
這篇文章主要介紹了IDEA在創(chuàng)建包時(shí)如何把包分開實(shí)現(xiàn)自動(dòng)分層,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法
這篇文章主要介紹了springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼)
這篇文章主要介紹了Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11