我有一个要从@ComponentScan
特定中排除的组件@Configuration
:
@Component("foo") class Foo {
...
}
否则,它似乎与我项目中的其他类发生冲突。我不完全理解碰撞,但如果我注释掉@Component
注释,事情就会像我想要的那样工作。但是其他依赖这个库的项目希望这个类由 Spring 管理,所以我只想在我的项目中跳过它。
我尝试使用@ComponentScan.Filter
:
@Configuration
@EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
但它似乎不起作用。如果我尝试使用FilterType.ASSIGNABLE_TYPE
,我会收到一个奇怪的错误,即无法加载一些看似随机的类:
原因:java.io.FileNotFoundException:类路径资源[junit/framework/TestCase.class]无法打开,因为它不存在
我也尝试使用type=FilterType.CUSTOM
如下:
class ExcludeFooFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
return metadataReader.getClass() == Foo.class;
}
}
@Configuration @EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.CUSTOM, value=ExcludeFooFilter.class)})
public class MySpringConfiguration {}
但这似乎并没有像我想要的那样从扫描中排除组件。
我该如何排除它?