最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot掃描自定義的servlet和filter代碼詳解

 更新時(shí)間:2017年10月10日 14:41:57   作者:葉長風(fēng)  
本文是一篇根據(jù)作者工作經(jīng)歷總結(jié)出來的關(guān)于springboot掃描自定義的servlet和filter代碼詳解的文章,小編覺得非常不錯(cuò),這里給大家分享下,和朋友們一起學(xué)習(xí),進(jìn)步。

這幾天使用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)行注冊才能使用,但是spring boot里面提供了一個(gè)注解來替代,為@ServletComponentScan,這個(gè)注解直接加在對應(yīng)的Application啟動類上即可,如下:

@SpringBootApplication
@ServletComponentScan
@ComponentScan
public class SpringBootWebApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootWebApplication.class, args);
  }
}

這樣編寫完之后,如果對應(yīng)的filter是在自己當(dāng)前模塊下的某個(gè)package中的時(shí)候是可以起作用的,但是如果本身項(xiàng)目中有多個(gè)模塊的時(shí)候,如果filter在一個(gè)類似與core下的package中,這樣注解加上去并沒有多大用處,最后會發(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代碼詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:淺談Java注解和動態(tài)代理 、Java之Spring注解配置bean實(shí)例代碼解析淺談Springboot之于Spring的優(yōu)勢等。有什么問題可以隨時(shí)留言,小編會及時(shí)回復(fù)大家。同時(shí)希望朋友們對腳本之家網(wǎng)站多多支持!

相關(guān)文章

  • IDEA創(chuàng)建Spring項(xiàng)目無法選擇Java8的問題及解決

    IDEA創(chuàng)建Spring項(xiàng)目無法選擇Java8的問題及解決

    文章描述了在使用Spring創(chuàng)建項(xiàng)目時(shí)遇到的問題,通過將服務(wù)器地址從https://start.spring.io/替換為https://start.aliyun.com/,成功解決了無法選擇Java8的問題
    2025-01-01
  • Java實(shí)現(xiàn)文件上傳下載以及查看功能

    Java實(shí)現(xiàn)文件上傳下載以及查看功能

    這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)文件上傳和下載以及查看功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Maven項(xiàng)目如何查找jar包是由哪個(gè)依賴引入的

    Maven項(xiàng)目如何查找jar包是由哪個(gè)依賴引入的

    這篇文章主要介紹了Maven項(xiàng)目如何查找jar包是由哪個(gè)依賴引入的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Hadoop組件簡介

    Hadoop組件簡介

    Hadoop作為一種分布式基礎(chǔ)架構(gòu),可以使用戶在不了解分布式底層細(xì)節(jié)的情況下,開發(fā)分布式程序。接下來通過本文給大家分享Hadoop組件簡介,感興趣的朋友一起看看吧
    2017-09-09
  • Mybatis的詳細(xì)使用教程

    Mybatis的詳細(xì)使用教程

    這篇文章主要介紹了Mybatis的詳細(xì)使用教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • Java并發(fā)編程之詳解CyclicBarrier線程同步

    Java并發(fā)編程之詳解CyclicBarrier線程同步

    在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口,ArrayBlockingQueue,DelayQueue,LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue,BlockingDeque接口,ConcurrentHashMap,CountDownLatch,本文為系列文章第十篇,需要的朋友可以參考下
    2021-06-06
  • 在Mybatis中association標(biāo)簽多層嵌套的問題

    在Mybatis中association標(biāo)簽多層嵌套的問題

    這篇文章主要介紹了在Mybatis中association標(biāo)簽多層嵌套的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼

    Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼

    這篇文章主要為大家詳細(xì)介紹了Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Jackson的用法實(shí)例分析

    Jackson的用法實(shí)例分析

    這篇文章主要介紹了Jackson的用法實(shí)例分析,用于處理Java的json格式數(shù)據(jù)非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • 使用Spring底層組件實(shí)現(xiàn)Aware接口

    使用Spring底層組件實(shí)現(xiàn)Aware接口

    這篇文章主要介紹了使用Spring底層組件實(shí)現(xiàn)Aware接口,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07

最新評論

突泉县| 山阴县| 利辛县| 扬中市| 固阳县| 东方市| 建水县| 富源县| 多伦县| 宜兴市| 淮南市| 色达县| 昌平区| 安义县| 新河县| 阿坝县| 辽宁省| 龙里县| 合肥市| 西乡县| 射阳县| 昭觉县| 和静县| 铁力市| 沙坪坝区| 岳池县| 昌图县| 洛南县| 金昌市| 礼泉县| 和平区| 琼结县| 镇平县| 榕江县| 永修县| 漠河县| 平塘县| 海门市| 房产| 古浪县| 正镶白旗|