[SOLVED] What @EnableAutoConfiguration actually “auto-configured”?

Issue

This Content is from Stack Overflow. Question asked by Will Huang

I’m reading Difference Between @ComponentScan and @EnableAutoConfiguration in Spring Boot article. See this:

When we define the spring-boot-starter-web dependency in our classpath, Spring boot auto-configures Tomcat and Spring MVC.

I can’t figure out what exact things been configured. Does it mean register DI container? Or something else? Is there any example explain this?



Solution

@EnableAutoConfiguration turns on auto configuration. Auto configuration tries to locate spring beans that should be configured for your application based on dependencies find in your classpath. Spring search for META-INF/spring.factories files. When it is enabled auto configuration class pointed by the property is loaded.

Consider below code snippet from spring source code:

@AutoConfiguration(
after = {DataSourceAutoConfiguration.class}
) 
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({JdbcProperties.class})
@Import({DatabaseInitializationDependencyConfigurer.class, 
         JdbcTemplateConfiguration.class, 
         NamedParameterJdbcTemplateConfiguration.class})
public class JdbcTemplateAutoConfiguration {
  public JdbcTemplateAutoConfiguration() {
  }
}

@ComponentScan on the other hand search for beans of your application code marked with stereotype annotations (@Component, @Controller, @Service, @Repository)


This Question was asked in StackOverflow by Will Huang and Answered by YogendraR It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?