Difference between @SpringBootApplication vs @EnableAutoConfiguration annotations in Spring Boot

Even though both @SpringBootApplication and @EnableAutoConfiguration can be used to enable the auto-configuration feature of Spring Boot there is a subtle difference between them. The @SpringBootApplication does much more than what @EnableAutoConfiguration do. It's actually a combination of three annotations: @Configuration which is used in Java-based configuration on Spring framework, @ComponentScan to enable component scanning of components you write e.g. @Controller classes, and @EnableAutoConfgiuration itself, which is used to enable auto-configuration in Spring Boot application. Spring Boot designers realize that these three annotations are frequently used together so they bundled them into @SpringBootApplicaiton. Now, instead of three annotations you just need to specify one annotation on your Main class.

What is the difference between @EnableAutoConfiguration and @SpringBootApplication is also a common Spring Boot interview question and it was recently asked one of my friends on his Java interview. Given the popularity of Spring Boot framework, it's good to know such questions before you go for your next Java interview.

In this article, I'll explain what does the @SpringBootApplication and @EnableAutoConfiguration do and highlight some important difference between them so that you can answer this question with confidence if you happen to see them in your interview.

1. What is Auto-Configuration in Spring Boot?

If you don't know, Spring Boot aims to simplify Java development with Spring framework. Currently, Spring does a lot for you but in return, it also asks a lot from you in terms of configuration and dependency management, Spring Boot aims to solve that problem.

The Spring Boto auto-configuration feature tries to automatically configure your Spring application based upon the JAR dependency you have added in the classpath.

For example, if HSQLDB is present on your classpath and you have not configured any database manually, Spring will auto-configure an in-memory database for you.

By default, this auto-configuration feature is not enabled and you need to opt-in for it by adding the @EnableAutoConfiguration or @SpringBootApplicaiton annotations to one of your @Configuration classes, generally the Main class which is used to run your application.

Though, this is just the tip of the iceberg. Spring Boot auto-configuration takes more than 200 such decision to configure your application, If you are interested in learning more then Spring Framework Master Class - Beginner to Expert is a good course to learn Spring Boot in depth.

Difference between @SpringBootApplication vs @EnableAutoConfiguration annotations in Spring Boot?

2. Difference between @EnableAutoConfiguration and @SpringBootApplication

Now that we know what is @SpringBootApplication and @EnableAutoConfiguration annotation and what do they do, it's time to highlight some important difference between them.

Here is a couple of worth-noting difference between @SpringBootApplication and @EnableAutoConfiguration annotations of Spring Boot:

1. Availability
The @SpringBootApplicaiton is relatively new than @EnableAutoConfiguration. It was introduced in Spring Boot 1.2 release while @EnableAutoConfiguation is present form the Spring Boot 1.0 release.

2. Purpose
The clear purpose of @EnableAutoConfiguration is to enable automatic configuration feature of Spring Boot application which automatically configures things if certain classes are present in Classpath e.g. it can configure Thymeleaf TemplateResolver and ViewResolver if Thymeleaf is present in the classpath.

On the other hand, @SpringBootApplication does three things, it allows you to run the Main class as a JAR with an embedded container. It enables Java configuration and it also enables Component Scanning.

3. Uses
It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in next point.

4. Control
The @EnableAutoConfiguration annotation allows you to selectively exclude certain classes from auto-configuration using exclude attribute as shown below:

public class MyConfiguration {
  //.. Java code

If the class is not on the classpath, you can use the excludeName attribute of the @EnableAutoConfiguration annotation and specify the fully qualified class name.

If you want to learn more about this annotation, I suggest you reading Craig Walls' (author of Spring in Action) another masterpiece Spring Boot in Action.

Difference between @SpringBootApplication vs @EnableAutoConfiguration annotations in Spring Boot?

3. Important points

Now that you understand the difference between @EnableAutoConfiguration and @SpringBootApplication, it's time to revise some important points about these two important annotations.

1. You should annotate the Main class or Bootstrap class with the @SpringBootApplication, this will allow you to run as a JAR with embedded web server Tomcat. If you want you can change that to Jetty or Undertow.

2. The @SpringBootApplication is a combination of three annotations @Configuration (used for Java-based configuration), @ComponentScan (used for component scanning), and @EnableAutoConfiguration (used to enable auto-configuration in Spring Boot).

3. The @EnableAutoConfiguration annotations enable auto-configuration features of Spring Boot which configures modules based on the presence of certain classes on the classpath. For example, if Thymeleaf JAR is present in classpath and Spring MVC is enabled e.g. using spring-boot-web-starter package then it can automatically configure template resolver and view resolver for you.

4. The @EnableAutoConfiguration annotation is based on @Conditional annotation of Spring 4.0 which enables conditional configuration.

5. In case of auto-configuration, manually declared beans can override beans automatically created by auto-configuration feature. This is achieved by using @ConditionalOnMissingBean of Spring 4.0

6. If you are using @EnableAutoConfiguration classes then you can selectively exclude certain classes from auto-configuration by using exclude as shown below:


7. The @SpringBootApplication annotation also provides aliases to customize the attributes of @EnableAutoConfiguration and @ComponentScan annotations.

That's all about the difference between @SpringBootApplication and @EnableAutoConfiguration annotations of Spring Boot. As you learned, the @SpringBootApplication makes it easy to enable auto-configuration and create a Bootstrap class by reducing the number of annotations you normally need i.e. instead of 3 annotations you just need one. But, if you need more control over autoconfiguration then you should use @EnableAutoConfiguration, which allows you to exclude classes from auto-configuration.

Further Learning
Spring Framework Master Class - Beginner to Expert
Master Microservices with Spring Boot and Spring Cloud
Master Hibernate and JPA with Spring Boot in 100 Steps
Spring Boot Reference Guide
REST with Spring MasterClass by Eugen Parsaschiv
Spring Boot: Efficient Development, Configuration, and Deployment

Thanks for reading this article so far. If you like this article and my explains of difference between @SpringBootApplication and @EnableAutoConfiguration then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments:

Post a Comment