Sunday, January 30, 2022

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

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 does. 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 like @Controller classes, and @EnableAutoConfgiuration itself, which is used to allow for auto-configuration in Spring Boot application.

Spring Boot designers quickly realized 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 by one of my friends on his Java interview. Given the popularity of the 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 differences between them so that you can answer this question with confidence if you happen to see them in your interview.

While it's not mandatory that you have to learn Spring Framework before Spring Boot, but I have found that a good knowledge of the Spring framework goes a long way in using Spring Boot correctly and troubleshooting any issues.

If you feel you need to brush up on your Core Spring skills or you want to learn the Spring framework in-depth, I suggest you go through the Spring Framework 5: Beginner to Guru course by John Thompson on Udemy. It's one of the most comprehensive and up-to-date courses to learn in Spring.





What is Auto-Configuration in Spring Boot?

If you don't know, Spring Boot aims to simplify Java development with the 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 decisions to configure your application, If you are interested in learning more about auto-configuration then Learn Spring Boot Rapidly course on Udemy is an excellent course to learn Spring Boot in depth.

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




Difference between @EnableAutoConfiguration and @SpringBootApplication?

Now that we know what @SpringBootApplication and @EnableAutoConfiguration annotation are and what do they do, it's time to highlight some important differences 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 from the Spring Boot 1.0 release.


2. Purpose

The apparent purpose of @EnableAutoConfiguration is to enable automatic configuration features of the 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 the next point.


4. Control

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

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
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 read Craig Walls' (author of Spring in Action) masterpiece, Spring Boot in Action.

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



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 like using a 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 the case of auto-configuration, manually declared beans can override beans automatically created by the 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:

@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)


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 you normally use in Java applications. As you have 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 mean, 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.



Other Java and Spring Articles you may like
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • 5 Free Courses to Learn Spring Framework for Beginners (free courses)
  • 5 Courses to Learn Spring Security for Beginners (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • 10 Free Courses to learn Spring Boot for Java developers (free courses)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • Top 5 Courses to learn Microservices in Java (courses)
  • 5 Spring Books Experienced Java Developer Should Read (books)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • 5 Courses to learn Spring Cloud for Beginners (courses)
  • 10 Advanced Spring Boot and Microservices Courses for Java devs (courses)

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

P. S. - If you are keen to learn Spring Boot from scratch but looking for free online training courses then you can also check out this free Introducing Spring Boot course on Udemy. It's completely free and you will just need a free Udemy account to join this course.

6 comments:

  1. hello,i have a doubt,according to your explanation,Instead of above 3 annotations,we have to use @springbootapplication right,but why we have to use @enableautoconfiguration for classpath.

    ReplyDelete
    Replies
    1. The exclusion option is not supported on @SpringBootApplicaiton, so if you need some advanced features like that you need to use dedicated annotations.

      Delete
    2. Is that really true? SpringBootApplication has an exclude argument that you can use to disable @Configuration classes.

      Delete
    3. public @interface SpringBootApplication {

      /**
      * Exclude specific auto-configuration classes such that they will never be applied.
      * @return the classes to exclude
      */
      @AliasFor(annotation = EnableAutoConfiguration.class)
      Class[] exclude() default {};

      Delete

Feel free to comment, ask questions if you have any doubt.