Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 596
e2rabi Avatar asked Jan 26 '16 00:01

e2rabi


People also ask

What is the difference between @SpringBootApplication and @EnableAutoConfiguration annotation?

The @EnableAutoConfiguration annotation is based on @Conditional annotation of Spring 4.0 which enables conditional configuration. 7. The @SpringBootApplication annotation also provides aliases to customize the attributes of @EnableAutoConfiguration and@ComponentScan annotations.

What is the difference between @component and @ComponentScan in Spring?

@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.

What does @EnableAutoConfiguration do in Spring boot?

Annotation Type EnableAutoConfiguration. Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, if you have tomcat-embedded.

What does @ComponentScan do in Spring boot?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.


2 Answers

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
like image 168
Shaheer Avatar answered Sep 24 '22 21:09

Shaheer


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();   }  } 
like image 23
ShayneR Avatar answered Sep 21 '22 21:09

ShayneR