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() {
}
}
ANNOTATION:注解类型ASSIGNABLE_TYPE:指定类型ASPECTJ:按照Aspectj表达式REGEX:按照正则表达式CUSTOM:自定义规则
烧不死的鸟就是凤凰

浙公网安备 33010602011771号