Example & Tutorial understanding programming in easy ways.

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

Even 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.

The @SpringBootApplication actually a combination of three annotations:

1.@Configuration which is used in Java-based configuration on Spring framework

2. @ComponentScan to enable component scanning of components you write e.g. @Controller classes

3. @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 we just need to specify one annotation on our Main class.@SpringBootApplication annotation.

Some differences -

1. Availability

The @SpringBootApplicaiton is 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. Itenables 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:

 

@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.

 

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 classwith 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 Under tow.

 

 

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 onthe presence of certain classes on the classpath. For example, if Thymeleaf JARis 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 beanscan override beans automatically created by auto-configuration feature. This isachieved 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.


 

Read More →