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: spring-framework 6.2.12)
package org.springframework.context.annotation;
   ........
public interface ImportSelector {
    String[] selectImports(AnnotationMetadata importingClassMetadata); 1
    @Nullable
    default Predicate<String> getExclusionFilter() {
        return null;
    } 2
}
1 Select and return the names of which class(es) should be imported based on the AnnotationMetadata of the importing @Configuration class.
Returns the class names, or an empty array if none
2 Return a predicate for excluding classes from the import candidates, to be transitively applied to all classes found through this selector's imports.

If this predicate returns true for a given fully-qualified class name, said class will not be considered as an imported configuration class, bypassing class file loading as well as metadata introspection.
Returns the filter predicate for fully-qualified candidate class names of transitively imported configuration classes, or null if none @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