激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - springboot掃描自定義的servlet和filter代碼詳解

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

2021-01-16 10:45葉長風(fēng) Java教程

本文是一篇根據(jù)作者工作經(jīng)歷總結(jié)出來的關(guān)于springboot掃描自定義的servlet和filter代碼詳解的文章,小編覺得非常不錯,這里給大家分享下,和朋友們一起學(xué)習(xí),進(jìn)步。

這幾天使用spring boot編寫公司一個應(yīng)用,在編寫了一個filter,用于指定編碼的filter,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * 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í)際使用的時候,卻是完全沒有起作用,后來查看了一下springboot的官方文檔,filter和servlet、listener之類的需要單獨(dú)進(jìn)行注冊才能使用,但是spring boot里面提供了一個注解來替代,為@ServletComponentScan,這個注解直接加在對應(yīng)的Application啟動類上即可,如下:

?
1
2
3
4
5
6
7
8
@SpringBootApplication
@ServletComponentScan
@ComponentScan
public class SpringBootWebApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootWebApplication.class, args);
  }
}

這樣編寫完之后,如果對應(yīng)的filter是在自己當(dāng)前模塊下的某個package中的時候是可以起作用的,但是如果本身項(xiàng)目中有多個模塊的時候,如果filter在一個類似與core下的package中,這樣注解加上去并沒有多大用處,最后會發(fā)現(xiàn)這個filter仍然沒有起作用。

我自己編寫的應(yīng)用有兩個,最開始的做法是把filter從core包中拆出來,然后在兩個模塊中各自添加一個,但是這樣未免有些代碼冗余,并且實(shí)現(xiàn)方式并不優(yōu)雅,然后我查看了下@ServletComponentScan的源碼,里面確實(shí)是有更好的解決方法。

@ServletComponentScan的源碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * 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 {};
}

這里有一個value()屬性,上面的注解默認(rèn)為basePackage,那么在掃描的時候就只掃描當(dāng)前模塊下面的包,其他不掃描,如果要連同其他模塊一起掃描的話,給這個屬性加上值即可,如下:

?
1
@ServletComponentScan(value = "cn.com")

如上,自定義的filter和servlet就可以正常起作用。

總結(jié)

以上就是本文關(guān)于springboot掃描自定義的servlet和filter代碼詳解的全部內(nèi)容,希望對大家有所幫助。有什么問題可以隨時留言,小編會及時回復(fù)大家。同時希望朋友們對服務(wù)器之家網(wǎng)站多多支持!

原文鏈接:http://blog.csdn.net/u012734441/article/details/53389351

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 特逼视频 | 成人午夜免费网站 | 91成人影库| 九九黄色 | 国产午夜亚洲精品午夜鲁丝片 | 91免费播放 | av在线播放观看 | 欧美wwwsss9999| 国产人妖一区二区 | 亚洲91精品 | 香蕉国产精品 | 沉沦的校花奴性郑依婷c到失禁 | 在线观看日韩中文字幕 | 一级做a爱片久久 | 国产精品一品二区三区四区18 | 欧美另类69xxxxx 视频 | 最新av在线播放 | 一本色道久久综合狠狠躁篇适合什么人看 | 亚洲激情91 | 久久精品伊人网 | 国产成人综合在线视频 | h色视频网站 | 99精品视频在线观看免费 | 91精品国产日韩91久久久久久360 | 欧美一级毛片美99毛片 | 午夜视频在线 | 精品国产一区二区三区四 | 免费一级片观看 | 欧美aaaaaaaa| 国产乱淫a∨片免费视频 | 亚洲成人在线视频网 | 国产91久久久久 | 一级黄色影片在线观看 | 91久久久久久久一区二区 | 国产毛片在线高清视频 | 国产自91精品一区二区 | 亚洲成人在线视频网站 | 欧美成人二区 | 欧美一区高清 | 看片一区| 久久成年网站 |