本文介绍了从@ComponentScan中排除@Component的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个组件,我想从@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 ,我会收到一个奇怪的错误,无法加载一些看似随机的类:

我还尝试使用 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 {}





如何排除它?


div class =h2_lin>解决方案

配置看起来很好,除了你应该使用 excludeFilters 而不是 / code>:

  @Configuration @EnableSpringConfigured 
@ComponentScan(basePackages = {com.example },excludeFilters = {
@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = Foo.class)})
public class MySpringConfiguration {}
pre>

I'm a Spring newbie, so please excuse me if the question appears nonsensical.

I have a component that I want to exclude from a @ComponentScan in a particular @Configuration:

@Component("foo") class Foo {
...
}

Otherwise, it seems to clash with some other class in my project. I don't fully understand the collision, but if I comment out the @Component annotation, things work like I want them to. But other projects that rely on this library expect this class to be managed by Spring, so I want to skip it only in my project.

I tried using @ComponentScan.Filter:

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

but it doesn't appear to work. If I try using FilterType.ASSIGNABLE_TYPE, I get a strange error about being unable to load some seemingly random class:

I also tried using type=FilterType.CUSTOM as following:

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 {}

But that doesn't seem to exclude the component from the scan like I want.

How do I exclude it?

解决方案

The configuration seem alright, except that you should use excludeFilters instead of excludes:

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

这篇关于从@ComponentScan中排除@Component的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 14:50