springboot scanning and custom servlet filter Code Explanation _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

These days companies use spring boot write an application, written in a filter, is used to specify the encoding of the filter, as follows:

/**
 * 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("force encoding ");
    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;
  }
}

However, in actual use, but it is absolutely not work, then I see a bit of official documents springboot, filter and servlet, like the listener needs to register in order to use alone, but spring boot which provides an annotation instead for @ServletComponentScan, this annotation applied directly to the Application startup class corresponding to, the following:

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

After completion of this writing, if the corresponding filter is the time in a package under their current modules can work, but if there are multiple modules in the project itself, when, if the filter in a similar package at the core , so to add annotations and of little use, and finally will find this filter is still not working.

Application I have written two, the beginning of the practice is to filter out the demolition from the core package, and then add a two modules each, but this is somewhat redundant code, and implementation is not elegant, then I View the next @ServletComponentScan the source code, which is indeed a better solution.

@ServletComponentScan source as follows:

/**
 * 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 {};
}

There is a value () attribute, defaults to the above comments basePackage, then when scanning module to scan only the current package below, the other does not scan, if you want to scan along with other modules together, then, to add value to this property ,as follows:

@ServletComponentScan(value = "cn.com")

As above, since the filter can be defined and servlet function properly.

to sum up

That's all for this article on springboot scan and custom servlet filter of explain the code, we want to help. Interested friends can see: On Java annotations and dynamic proxies, Java annotation of Spring bean configuration instance of code analysis, Talking Springboot advantage of the Spring and so on. What is the problem you can always leave a message, we will respond promptly small series. At the same time I hope you will learn a lot of support network for Hi!

The original address is: http: //www.piaodoo.com/thread-13237-1-2.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093975.html