您好,登錄后才能下訂單哦!
這篇文章主要介紹springboot掃描自定義servlet和filter的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
這幾天使用spring boot編寫(xiě)公司一個(gè)應(yīng)用,在編寫(xiě)了一個(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í)候,卻是完全沒(méi)有起作用,后來(lái)查看了一下springboot的官方文檔,filter和servlet、listener之類(lèi)的需要單獨(dú)進(jìn)行注冊(cè)才能使用,但是spring boot里面提供了一個(gè)注解來(lái)替代,為@ServletComponentScan,這個(gè)注解直接加在對(duì)應(yīng)的Application啟動(dòng)類(lèi)上即可,如下:
@SpringBootApplication @ServletComponentScan @ComponentScan public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } }
這樣編寫(xiě)完之后,如果對(duì)應(yīng)的filter是在自己當(dāng)前模塊下的某個(gè)package中的時(shí)候是可以起作用的,但是如果本身項(xiàng)目中有多個(gè)模塊的時(shí)候,如果filter在一個(gè)類(lèi)似與core下的package中,這樣注解加上去并沒(méi)有多大用處,最后會(huì)發(fā)現(xiàn)這個(gè)filter仍然沒(méi)有起作用。
我自己編寫(xiě)的應(yīng)用有兩個(gè),最開(kāi)始的做法是把filter從core包中拆出來(lái),然后在兩個(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就可以正常起作用。
以上是“springboot掃描自定義servlet和filter的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。