What is the difference between the `@ComponentScan` and `@EnableAutoConfiguration` annotations in Spring Boot? Is it necessary to add these? My application works very well without these annotations. I just want to understand why we have to add them.
['https://Stackoverflow.com/questions/35005158', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5258516/']
> 
> What is the difference between the @ComponentScan and
>  @EnableAutoConfiguration annotations in Spring Boot?
> 
> 
> 

[`@EnableAutoConfiguration`](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html) 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`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html) 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 pre-defined project package. If no package is specified current class package is taken as the root package. 

> 
> Is it necessary to add these?
> 
> 
> 

If you need Spring boot to Auto configure every thing for you [`@EnableAutoConfiguration`](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html) is required. You don't need to add it manually, spring will add it internally for you based on the annotation you provide.

Actually the [`@SpringBootApplication`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html) annotation is equivalent to using [`@Configuration`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html), [`@EnableAutoConfiguration`](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html) and [`@ComponentScan`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html) with their default attributes.

**See also:**

* [Using the @SpringBootApplication annotation](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html)
* [Auto-configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html)
**`@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 `spring-boot-starter-web` dependency in your classpath, it automatically configures Tomcat and Spring MVC.

```
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

```

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 fully qualified name of class.

Examples:

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

@EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

```

`@SpringBootApplication` is a newer version of `@EnableAutoConfiguration` which was introduced in Spring Boot 1.2.

`@SpringBootApplication` is a combination of three annotations,

* `@Configuration` - for java based configuration classes.
* `@ComponentScan` - to enable component scanning, all the packages and
subpackages will be auto-scanned which are under the root package on 
which @SpringBootApplication is applied.
* `@EnableAutoConfiguration` - to enable auto-configuration of the  

classes bases on the jars added in classpath.

**`@ComponentScan`** enables component scanning so that web controller classes and other components that you create will be automatically
discovered and registered as beans in spring's application context.
You can specify the base packages that will be scanned for auto-discovering and registering of beans.

One of the optional element is,

* basePackages - can be used to state specific packages to scan.

Example,

```
@ComponentScan(basePackages = {"com.example.test"})
@Configuration
public class SpringConfiguration { }

```