What is the difference between @ComponentScan and @EnableAutoConfiguration in Spring Boot?

What is the difference between the @ComponentScan and
@EnableAutoConfiguration annotations in Spring Boot?

@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 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 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 annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.

See also:

  • Using the @SpringBootApplication annotation
  • Auto-configuration

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)