117

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:

Caused by: java.io.FileNotFoundException: class path resource [junit/framework/TestCase.class] cannot be opened because it does not exist

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.CUSTOM, value=ExcludeFooFilter.class)})
public class MySpringConfiguration {}

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

How do I exclude it?

ykaganovich
  • 14,736
  • 8
  • 59
  • 96

8 Answers8

142

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 {}
Sergi Almar
  • 8,054
  • 3
  • 32
  • 30
  • sorry, that was a cut and paste error. I am using excludeFilters. I'll take another look, the error this gives me is really bizarre... – ykaganovich Sep 24 '13 at 22:48
  • in my case, FilterType.ASSIGNABLE_TYPE did not work - I think it only works when the classes you want to exclude are also `@Configuration` classes. but FilterType.ASPECTJ worked - specifically it worked even e.g. for individual classes - doesn't have to refer to packages. Also if you have multiple `@Configuration` classes further down in your class hierarchy, the exclusion filters have to be applied to main `@SpringBootApplication` class (which is also a `@Configuration`) - otherwise it does component scan and manages to instantiate classes before other filters start to work. – hello_earth Aug 13 '21 at 08:52
63

Using explicit types in scan filters is ugly for me. I believe more elegant approach is to create own marker annotation:

@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreDuringScan {
}

Mark component that should be excluded with it:

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

And exclude this annotation from your component scan:

@ComponentScan(excludeFilters = @Filter(IgnoreDuringScan.class))
public class MySpringConfiguration {}
Ralph
  • 118,862
  • 56
  • 287
  • 383
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 24
    That's a clever idea for universal exclusion though won't help if you want to exclude a component from only a subset of application contexts in a project. Really, to exclude it universally, one could just remove the `@Component`, but I don't think that's what the question is asking – Kirby May 12 '15 at 18:18
  • 4
    this won't work if you have another component scan annotation somewhere that doesn't have the same filter – Bashar Ali Labadi Dec 06 '18 at 20:14
  • 3
    @Bashar Ali Labadi, isn't that kind of point of this construct? If you want to exclude it from all component scans, it probably shouldn't be Spring component at all. – luboskrnac Dec 03 '19 at 14:31
36

Another approach is to use new conditional annotations. Since plain Spring 4 you can use @Conditional annotation:

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

and define conditional logic for registering Foo component:

public class FooCondition implements Condition{
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // return [your conditional logic]
    }     
}

Conditional logic can be based on context, because you have access to bean factory. For Example when "Bar" component is not registered as bean:

    return !context.getBeanFactory().containsBean(Bar.class.getSimpleName());

With Spring Boot (should be used for EVERY new Spring project), you can use these conditional annotations:

  • @ConditionalOnBean
  • @ConditionalOnClass
  • @ConditionalOnExpression
  • @ConditionalOnJava
  • @ConditionalOnMissingBean
  • @ConditionalOnMissingClass
  • @ConditionalOnNotWebApplication
  • @ConditionalOnProperty
  • @ConditionalOnResource
  • @ConditionalOnWebApplication

You can avoid Condition class creation this way. Refer to Spring Boot docs for more detail.

mkobit
  • 43,979
  • 12
  • 156
  • 150
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 1
    +1 for the conditionals, this is a much cleaner way to me than using filters. I haven't ever gotten filters to work as consistently as conditional loading of beans – wondergoat77 Dec 27 '17 at 16:52
25

In case you need to define two or more excludeFilters criteria, you have to use the array.

For instances in this section of code I want to exclude all the classes in the org.xxx.yyy package and another specific class, MyClassToExclude

 @ComponentScan(            
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.xxx.yyy.*"),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MyClassToExclude.class) })
Enrico Giurin
  • 2,183
  • 32
  • 30
6

I had an issue when using @Configuration, @EnableAutoConfiguration and @ComponentScan while trying to exclude specific configuration classes, the thing is it didn't work!

Eventually I solved the problem by using @SpringBootApplication, which according to Spring documentation does the same functionality as the three above in one annotation.

Another Tip is to try first without refining your package scan (without the basePackages filter).

@SpringBootApplication(exclude= {Foo.class})
public class MySpringConfiguration {}
dorony
  • 1,008
  • 1
  • 14
  • 31
  • 7
    I have tried this but showing error as "The following classes could not be excluded because they are not auto-configuration classes". excludeFilters with @ComponentScan is working fine. – AzarEJ Sep 29 '20 at 07:57
  • 1
    This is wrong. `Foo` can only be an auto configuration class. – Abhijit Sarkar Aug 12 '21 at 22:26
1

In case of excluding test component or test configuration, Spring Boot 1.4 introduced new testing annotations @TestComponent and @TestConfiguration.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
1

Your custom filer has wrong logic. Because metadataReader.getClass() is always not equal to Foo.class, metadataReader.getClass() return "org.springframework.core.type.classreading.MetadataReader". It'd use className for comparation.

public class CustomFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
    return metadataReader.getClassMetadata().getClassName().equals(Foo.class.getName());
}

}

@ComponentScan.Filter is belong every @ComponentScan and only filter compononets from the specific scan path.

If you find it doesn't work, you could check whether there's another @ComponentScan or <context:component-scan base-package="xxx"/> in your code

dknight
  • 619
  • 6
  • 7
0

I needed to exclude an auditing @Aspect @Component from the app context but only for a few test classes. I ended up using @Profile("audit") on the aspect class; including the profile for normal operations but excluding it (don't put it in @ActiveProfiles) on the specific test classes.

Miguel Pereira
  • 1,781
  • 16
  • 14