温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot扫描自定义servlet和filter的示例分析

发布时间:2021-07-08 11:28:44 来源:亿速云 阅读:115 作者:小新 栏目:编程语言

这篇文章主要介绍springboot扫描自定义servlet和filter的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

这几天使用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的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI