SpringBoot—@ComponentScan注解过滤排除某个类

技术公众号:后端技术解忧铺
关注微信公众号:CodingTechWork,一起学习进步。

问题

  在抽取公共swagger配置类时,将swagger放入com.test.common.config包内,其他模块通过@ComponentScan进行进行引用,但有的模块在引用时,会扫描到common.config包路径下的其他配置类而引发错误,如引用到RedisConfig类而报错,此时需要将该类排除掉。

解决方案

  通过@ComponentScan中的excludeFilters属性进行排除类。

@SpringBootApplication
@ComponentScan(basePackages = {"com.test.common.config"},
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {RedisConfig.class})})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

附:FilterType

package org.springframework.context.annotation;

public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;

    private FilterType() {
    }
}
  1. ANNOTATION:注解类型
  2. ASSIGNABLE_TYPE:指定类型
  3. ASPECTJ:按照Aspectj表达式
  4. REGEX:按照正则表达式
  5. CUSTOM:自定义规则
posted @ 2021-06-21 17:38  Andya_net  阅读(239)  评论(0编辑  收藏  举报  来源