Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

The basic difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.

Table of Content :

@EnableAutoConfiguration annotation tells Spring Boot to "guess" how you will want to configure Spring, based on the jar dependencies that you have added. For example, If Hsqldb is on your classpath, and you have not manually configured any database connection beans, then Spring will auto-configure an in-memory database.

@ComponentScan tells Spring to look for other components, configurations, and services in the specified package. Spring is able to auto scan, detect and register your beans or components from a pre-defined project package. If no package is specified current class package is taken as the root package.

@ComponentScan provides scope for spring component scan, it simply goes through the provided base package and picks up dependencies required by @Bean or @Autowired, etc, 

In a typical Spring application, @ComponentScan is used in configuration classes, the ones annotated with @Configuration.

The Configuration classes contain methods annotated with @Bean. These @Bean annotated methods generate beans managed by Spring container. Those beans will be auto-detected by @ComponentScan annotation.

There are some annotations that make beans auto-detectable like @Repository, @Service, @Controller, @Configuration, @Component. In the below code Spring starts scanning from the package including BeanA class.

@Configuration @ComponentScan(basePackageClasses = BeanA.class) @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) public class Config { @Bean public BeanA beanA(){ return new BeanA(); } @Bean public BeanB beanB{ return new BeanB(); } }

If you need Spring boot to Auto configure everything for you @EnableAutoConfiguration is required. You don't need to add it manually, spring will add it internally for you based on the annotation you provide.

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.

@EnableAutoConfiguration in spring boot tells how you want to configure spring, based on the jars that you have added in your classpath. For example, if you add the spring-boot-starter-web dependency in your classpath, it automatically configures Tomcat and Spring MVC.

org.springframework.boot spring-boot-starter-web

You can use @EnableAutoConfiguration annotation along with @Configuration annotation. It has two optional elements,

exclude: if you want to exclude the auto-configuration of a class.

 excludeName : if you want to exclude the auto-configuration of a class using a fully qualified name of the class. Examples:

@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }


Difference Between @ComponentScan and @EnableAutoConfiguration in
Spring Boot

EnableAutoConfiguration ComponentScan
EnableAutoConfiguration annotation tells Spring Boot to "guess" how you will want to configure Spring, based on the jar dependencies that you have added. ComponentScan tells Spring to look for other components, configurations, and services in the specified package.
For example, If Hsqldb is on your classpath, and you have not manually configured any database connection beans, then Spring will auto-configure an in-memory database. Spring is able to auto scan, detect and register your beans or components from a pre-defined project package. If no package is specified current class package is taken as the root package.
EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications. ComponentScan scans for Spring components.


In this article, we have seen What is the Difference Between @ComponentScan and @EnableAutoConfiguration in Spring Boot.