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

WX: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:自定义规则
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容