Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does @Import do, and what kinds of classes can it import?

@Import tells Spring to add specific classes to the configuration it is processing, as if they had been registered directly. It accepts @Configuration classes, plain component classes, ImportSelector implementations that return class names to import, and ImportBeanDefinitionRegistrar implementations that register bean definitions in code. The @Enable annotations such as @EnableScheduling are built on it, and it gives us explicit composition instead of component scanning.

Why this question is asked

Most Spring Boot applications rely on component scanning and auto-configuration, so many developers never write @Import themselves. Yet it is the mechanism behind every @Enable annotation and behind auto-configuration, and it is the tool we reach for when we want a configuration to say exactly what it contains. Interviewers ask about it to see whether we understand how configuration is assembled rather than only that it works. In this answer, we will recap how Spring processes configuration classes, look at each kind of class @Import accepts with an example, see how @Enable annotations use it, and compare it with component scanning.

1A Quick Recap of Configuration Processing

When the application context starts, a component called ConfigurationClassPostProcessor reads our configuration classes. For each one it follows @ComponentScan, @Bean methods, @PropertySource and @Import, and turns what it finds into bean definitions. A bean definition is the recipe Spring keeps for a bean: its class, scope and dependencies. Only after all definitions are collected does Spring create the beans.

Component scanning finds classes by searching packages on the classpath for annotations such as @Component. @Import works differently: it names the classes directly. So the configuration tells the reader, and Spring, exactly which classes it pulls in, wherever they live.

2Importing Configuration and Component Classes

The simplest use is to import another @Configuration class. Its @Bean methods are processed as if they were written in the importing class.

Java
@Configuration
class DataSourceConfig {
@Bean DataSource dataSource() { return new HikariDataSource(); }
}
@Component
class AuditClock {} // not in a scanned package
@Configuration
@Import({DataSourceConfig.class, AuditClock.class})
class AppConfig {}
var ctx = new AnnotationConfigApplicationContext(AppConfig.class);
ctx.getBean(DataSource.class); // HikariDataSource
ctx.getBean(AuditClock.class); // AuditClock, registered without scanning

Since Spring 4.2, @Import also accepts ordinary classes that are not configuration classes. Spring registers them as beans, just as if we had called register() on the context. Notice that AuditClock was found even though nothing scanned its package. That is the point of @Import: the dependency is written down instead of discovered.

3ImportSelector: Choosing Classes in Code

Sometimes the classes to import depend on something, such as an attribute of an annotation or what is on the classpath. An ImportSelector is a class whose selectImports() method returns the names of the classes to import. Spring calls it while processing configuration and then imports whatever it returned.

Java
class CacheSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importing) {
var attrs = importing.getAnnotationAttributes(EnableShopCache.class.getName());
boolean redis = (boolean) attrs.get("redis");
return new String[] { redis ? RedisCacheConfig.class.getName() : LocalCacheConfig.class.getName() };
}
}
@Retention(RetentionPolicy.RUNTIME)
@Import(CacheSelector.class)
@interface EnableShopCache { boolean redis() default false; }
@Configuration @EnableShopCache(redis = true) class AppConfig {} // imports RedisCacheConfig

The AnnotationMetadata argument describes the class that carried the annotation, so the selector can read its attributes. A variant called DeferredImportSelector runs only after all other configuration has been processed. Spring Boot's auto-configuration uses that variant so that our own beans are known before it decides what to add.

4ImportBeanDefinitionRegistrar: Registering Definitions Directly

The most flexible option is ImportBeanDefinitionRegistrar. Instead of returning class names, it receives the BeanDefinitionRegistry and registers bean definitions itself. This is what we need when the number or the shape of the beans is not known in advance, for example one bean per interface found in a package.

Java
class ClientRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) {
for (String name : List.of("billing", "shipping")) {
var def = BeanDefinitionBuilder.genericBeanDefinition(RemoteClient.class)
.addConstructorArgValue(name).getBeanDefinition();
registry.registerBeanDefinition(name + "Client", def); // billingClient, shippingClient
}
}
}
What @Import accepts
KindWhat Spring does with itTypical use
@Configuration classprocesses its @Bean methods and annotationssplit one config into parts
Plain component classregisters it as a bean (Spring 4.2+)add one class without scanning
ImportSelectorimports the class names it returnschoose configs from annotation attributes
DeferredImportSelectorsame, after all other config is processedBoot auto-configuration
ImportBeanDefinitionRegistrarlets it register bean definitions in codeone bean per interface, as Spring Data does

Spring Data uses a registrar for @EnableJpaRepositories: it scans for repository interfaces and registers one factory bean per interface. So the four kinds form a ladder from plain to programmable: a configuration class, a component class, a selector that picks classes, and a registrar that writes definitions.

5How @Enable Annotations Use @Import

Every @Enable annotation is a small wrapper around @Import. @EnableScheduling is annotated with @Import(SchedulingConfiguration.class), which registers the post-processor that finds @Scheduled methods. @EnableAsync imports a selector that picks proxy-based or AspectJ-based configuration depending on its mode attribute. @EnableJpaRepositories imports a registrar.

So when we write @EnableScheduling, we are really importing infrastructure configuration that someone else wrote. This is also how we can build our own: a company library can offer @EnableCompanyTracing that imports its configuration, and an application opts in with one line.

6@Import Compared With Component Scanning

Component scanning is convenient: we annotate a class and it appears. The cost is that the set of beans depends on which packages are scanned and what happens to be in them, so adding a jar or moving a class can silently add or remove beans. @Import is explicit composition: the importing class lists what it brings in, and a reader can follow the list.

In practice we use both. Scanning works well inside one application's own packages. @Import is better at boundaries: for libraries, for modules that expose one entry configuration, and in tests where we want a small, predictable context. Note also that @Import pulls in Java classes; to bring in an XML configuration file we use @ImportResource instead.

7Follow-up questions

What happens if the same configuration class is imported twice?

Spring processes each configuration class once, so importing it from two places does not create duplicate beans. That makes it safe for two modules to import a shared configuration.

Can an ImportSelector see the beans already defined?

A plain ImportSelector runs while configuration is still being parsed, so it should not rely on other bean definitions. A DeferredImportSelector runs after all configuration classes have been processed, which is why conditions such as @ConditionalOnMissingBean in auto-configuration can see the application's own beans.

How is @Import different from @ComponentScan on the imported package?

@ComponentScan picks up every annotated class in a package, including ones added later. @Import brings in only the named classes. If the package grows, scanning grows with it, and @Import does not, which is the explicitness we want at module boundaries.

8Conclusion

In this answer, we saw that @Import adds named classes to the configuration being processed. It accepts @Configuration classes, plain component classes since Spring 4.2, ImportSelector implementations that choose classes in code, including the deferred kind that Boot auto-configuration uses, and ImportBeanDefinitionRegistrar implementations that register definitions directly. @Enable annotations are thin wrappers over it, and it gives explicit composition where component scanning gives discovery.

What to say in the interviewAbout 80 seconds aloud

Open with

@Import adds specific classes to the configuration Spring is processing, so they are handled as if they were registered directly. It can import four kinds of classes. First, other @Configuration classes, whose @Bean methods then become part of the context.

Then, if they want more

Second, since Spring 4.2, plain component classes, which are simply registered as beans without any scanning. Third, an ImportSelector, which returns class names in code, for example based on attributes of the annotation that triggered it; the deferred variant runs after all user configuration and is how Spring Boot loads auto-configuration.

Fourth, an ImportBeanDefinitionRegistrar, which gets the registry and registers bean definitions itself, which is how Spring Data registers one repository bean per interface. The @Enable annotations are built on this: @EnableScheduling just imports SchedulingConfiguration, and @EnableAsync imports a selector that picks proxy or AspectJ mode.

The difference from component scanning is that @Import is explicit: the configuration lists exactly what it brings in, so adding a class to a package does not silently change the context. I use scanning inside an application's own packages and @Import at boundaries, for libraries, module entry points and small test contexts.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does @Import do, and what kinds of classes can it import?": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/bb49999c29/