欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot扫描自定义的servlet和filter代码详解

程序员文章站 2024-02-25 21:57:33
这几天使用spring boot编写公司一个应用,在编写了一个filter,用于指定编码的filter,如下: /** * created by xiax...

这几天使用spring boot编写公司一个应用,在编写了一个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;
  }
}

但是在实际使用的时候,却是完全没有起作用,后来查看了一下springboot的官方文档,filter和servlet、listener之类的需要单独进行注册才能使用,但是spring boot里面提供了一个注解来替代,为@servletcomponentscan,这个注解直接加在对应的application启动类上即可,如下:

@springbootapplication
@servletcomponentscan
@componentscan
public class springbootwebapplication {
  public static void main(string[] args) {
    springapplication.run(springbootwebapplication.class, args);
  }
}

这样编写完之后,如果对应的filter是在自己当前模块下的某个package中的时候是可以起作用的,但是如果本身项目中有多个模块的时候,如果filter在一个类似与core下的package中,这样注解加上去并没有多大用处,最后会发现这个filter仍然没有起作用。

我自己编写的应用有两个,最开始的做法是把filter从core包中拆出来,然后在两个模块中各自添加一个,但是这样未免有些代码冗余,并且实现方式并不优雅,然后我查看了下@servletcomponentscan的源码,里面确实是有更好的解决方法。

@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 {};
}

这里有一个value()属性,上面的注解默认为basepackage,那么在扫描的时候就只扫描当前模块下面的包,其他不扫描,如果要连同其他模块一起扫描的话,给这个属性加上值即可,如下:

@servletcomponentscan(value = "cn.com")

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

总结

以上就是本文关于springboot扫描自定义的servlet和filter代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:浅谈java注解和动态代理 、java之spring注解配置bean实例代码解析浅谈springboot之于spring的优势等。有什么问题可以随时留言,小编会及时回复大家。同时希望朋友们对网站多多支持!