Spring Framework
Spring Boot
@Import
@ImportAutoConfiguration
Java Annotations

What is difference between ImportAutoConfiguration and Import

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

@Import and @ImportAutoConfiguration both bring configuration into the Spring application context, but they solve different problems. @Import is a core Spring mechanism for explicitly importing known configuration classes or selectors, while @ImportAutoConfiguration is a Spring Boot feature for selectively applying Boot auto-configuration.

Use @Import for Explicit Configuration

@Import is part of the core Spring Framework. You use it when you know exactly which configuration classes, registrars, or import selectors you want to include.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Import;
4
5@Configuration
6class DatabaseConfig {
7    @Bean
8    public String databaseClient() {
9        return "db-client";
10    }
11}
12
13@Configuration
14@Import(DatabaseConfig.class)
15class AppConfig {
16}

This is explicit and direct. Spring simply imports the supplied configuration class into the context.

@Import is a good fit when:

  • You own the configuration classes
  • You want precise control over what is included
  • You are composing modular application configuration

Use @ImportAutoConfiguration for Boot Auto-Config

@ImportAutoConfiguration belongs to Spring Boot, not plain Spring Framework. It is designed for importing auto-configuration classes, typically the same kind of classes Boot would otherwise discover automatically through its auto-configuration mechanism.

java
1import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6@ImportAutoConfiguration(JacksonAutoConfiguration.class)
7class JsonTestConfig {
8}

This is especially useful in tests, custom test slices, or narrowly scoped Boot setups where you want selected auto-configuration behavior without enabling everything.

Think of the Difference in Intent

A useful mental model is:

  • '@Import: "include exactly these configuration types"'
  • '@ImportAutoConfiguration: "apply Boot auto-configuration logic for these auto-config classes"'

That difference in intent matters because Boot auto-config classes participate in conditional configuration, ordering, and the broader auto-configuration model. They are not just ordinary app config classes you happen to import.

Common Use Cases

For application code, @Import is common when splitting configuration across modules.

For testing, @ImportAutoConfiguration is often the more natural tool when you want a small slice of Boot behavior. For example, you may want Jackson support, web MVC infrastructure, or a particular metrics configuration in a test context without pulling in the full application.

That is why @ImportAutoConfiguration appears frequently in Spring Boot testing annotations and custom test support code.

Another practical difference is that auto-configuration classes are designed to be conditional. Importing them through Boot's auto-config path lets those conditions participate as intended, rather than treating them like ordinary user configuration classes.

Common Pitfalls

The biggest mistake is treating the annotations as interchangeable. They are related, but not equivalent. @Import brings in specific types directly; @ImportAutoConfiguration is about Boot's auto-configuration layer and is typically used with auto-config classes.

Another issue is using @ImportAutoConfiguration in plain Spring projects that are not actually using Spring Boot's auto-configuration model. In that context, it usually adds confusion instead of clarity.

Developers also sometimes import ordinary application configuration classes with @ImportAutoConfiguration. That is usually the wrong abstraction. If the class is just your own @Configuration, @Import is the clearer choice.

Finally, avoid overusing either annotation when component scanning or existing Boot conventions already provide the desired behavior. Additional explicit imports should solve a real configuration need, not duplicate what the framework is already doing.

Summary

  • '@Import is a core Spring annotation for explicitly importing known configuration types.'
  • '@ImportAutoConfiguration is a Spring Boot annotation for selectively applying auto-configuration classes.'
  • Use @Import for modular application configuration you control directly.
  • Use @ImportAutoConfiguration mainly when working with Boot auto-config, especially in tests or targeted contexts.
  • Choose based on intent: explicit config composition versus selective Boot auto-configuration.

Course illustration
Course illustration

All Rights Reserved.