The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.
For example, when we define the spring-boot-starter-web dependency in our classpath, Spring boot auto-configures Tomcat and Spring MVC. However, this auto-configuration has less precedence in case we define our own configurations.
The package of the class declaring the @EnableAutoConfiguration annotation is considered as the default. Therefore, we should always apply the @EnableAutoConfiguration annotation in the root package so that every sub-packages and class can be examined:
@Configuration @EnableAutoConfiguration public class EmployeeApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args); // ... } }
Furthermore, the @EnableAutoConfiguration annotation provides two parameters to manually exclude any parameter:
We can use exclude to disable a list of classes that we do not want to be auto-configured:
@Configuration @EnableAutoConfiguration(exclude={JdbcTemplateAutoConfiguration.class}) public class EmployeeApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args); // ... } }
We can use excludeName to define a fully qualified list of class names that we want to exclude from the auto-configuration:
@Configuration @EnableAutoConfiguration(excludeName = {"org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"}) public class EmployeeApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args); // ... } }
Since Spring Boot 1.2.0, we can use the @SpringBootApplication annotation, which is a combination of the three annotations @Configuration, @EnableAutoConfiguration, and@ComponentScan with their default attributes:
@SpringBootApplication public class EmployeeApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EmployeeApplication.class, args); // ... } }
Reference :