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/']
One of the main advantages of Spring Boot is its annotation driven versus traditional xml based configurations, **@EnableAutoConfiguration** automatically configures the Spring application based on its included jar files, it sets up defaults or helper based on dependencies in pom.xml.
Auto-configuration is usually applied based on the classpath and the defined beans. Therefore, we donot need to define any of the DataSource, EntityManagerFactory, TransactionManager etc and magically based on the classpath, Spring Boot automatically creates proper beans and registers them for us. For example when there is a tomcat-embedded.jar on your classpath you likely need a TomcatEmbeddedServletContainerFactory (unless you have defined your own EmbeddedServletContainerFactory bean). @EnableAutoConfiguration has a exclude attribute to disable an auto-configuration explicitly otherwise we can simply exclude it from the pom.xml, for example if we donot want Spring to configure the tomcat then exclude spring-bootstarter-tomcat from spring-boot-starter-web.

**@ComponentScan** provides scope for spring component scan, it simply goes though *the provided base package* and picks up dependencies required by @Bean or @Autowired etc, In a typical Spring application, @ComponentScan is used in a configuration classes, the ones annotated with @Configuration. Configuration classes contains 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 which make beans auto-detectable like @Repository , @Service, @Controller, @Configuration, @Component.
In 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();
  }

}

```
**`@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 { }

```