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() {
}
}
-
ANNOTATION
:注解类型 -
ASSIGNABLE_TYPE
:指定类型 -
ASPECTJ
:按照Aspectj表达式 -
REGEX
:按照正则表达式 -
CUSTOM
:自定义规则