Close

Spring - @Configuration selection by using ImportSelector

[Last Updated: Nov 6, 2025]

@Import annotation can be used for importing bean definitions from other @Configuration classes.

@Import annotation can also be configured with an ImportSelector implementation to select @Configuration classes programmatically, based on some selection criteria.

Definition of ImportSelector

Version: 7.0.4
 package org.springframework.context.annotation;
 public interface ImportSelector {
     String[] selectImports(AnnotationMetadata importingClassMetadata); 1
     default Predicate<String> getExclusionFilter(); 2
 }
1Select and return the names of which class(es) should be imported based on the AnnotationMetadata of the importing @Configuration class.
2Return a predicate for excluding classes from the import candidates, to be transitively applied to all classes found through this selector's imports. (Since 5.2.4)

ImportSelector use cases

ImportSelector can be used for dynamic configuration selection rather than static @Profile based selection.

Spring framework itself uses ImportSelector at many places to ease the configuration burden on the client side, for example @EnableAsync, @EnableScheduling etc.

In the next tutorials we will see how to use ImportSelector

See Also

Join