相关疑难解决方法(0)

从@ComponentScan中排除@Component

我有一个组件,我想从@ComponentScan特定的一个组件中排除@Configuration:

@Component("foo") class Foo {
...
}
Run Code Online (Sandbox Code Playgroud)

否则,它似乎与我项目中的其他类冲突.我不完全理解碰撞,但是如果我注释掉@Component注释,事情就像我想要的那样.但是依赖这个库的其他项目希望这个类由Spring管理,所以我想在我的项目中跳过它.

我试过用@ComponentScan.Filter:

@Configuration 
@EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
  @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用.如果我尝试使用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.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)

但这似乎并没有像我想要的那样从扫描中排除组件.

我如何排除它?

java spring dependency-injection spring-ioc spring-boot

76
推荐指数
5
解决办法
12万
查看次数