Difference Betwixt @Springbootapplication Vs @Enableautoconfiguration Annotations Inward Jump Boot

Even though both @SpringBootApplication together with @EnableAutoConfiguration tin live on used to enable the auto-configuration characteristic of Spring Boot at that topographic point is a subtle departure betwixt them. The @SpringBootApplication does much to a greater extent than than what @EnableAutoConfiguration do. It's genuinely a combination of iii annotations: @Configuration which is used inwards Java-based configuration on Spring framework, @ComponentScan to enable element scanning of components yous write e.g. @Controller classes, together with @EnableAutoConfgiuration itself, which is used to enable auto-configuration inwards Spring Boot application. Spring Boot designers realize that these iii annotations are oft used together thence they bundled them into @SpringBootApplicaiton. Now, instead of iii annotations yous simply demand to specify 1 annotation on your Main class.

What is the departure betwixt @EnableAutoConfiguration together with @SpringBootApplication is too a mutual Spring Boot interview query together with it was of late asked 1 of my friends on his Java interview. Given the popularity of Spring Boot framework, it's practiced to know such questions earlier yous acquire for your side past times side Java interview.

In this article, I'll explicate what does the @SpringBootApplication together with @EnableAutoConfiguration do together with highlight to a greater extent than or less of import departure betwixt them thence that yous tin respond this query amongst confidence if yous conduct house to encounter them inwards your interview.




1. What is Auto-Configuration inwards Spring Boot?

If yous don't know, Spring Boot aims to simplify Java evolution amongst Spring framework. Currently, Spring does a lot for yous but inwards return, it too asks a lot from yous inwards price of configuration together with 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 yous conduct maintain added inwards the classpath.

For example, if HSQLDB is nowadays on your classpath together with yous conduct maintain non configured whatsoever database manually, Spring volition auto-configure an in-memory database for you.

By default, this auto-configuration characteristic is non enabled together with yous demand to opt-in for it past times adding the @EnableAutoConfiguration or @SpringBootApplicaiton annotations to 1 of your @Configuration classes, mostly the Main cast which is used to run your application.

Though, this is simply the tip of the iceberg. Spring Boot auto-configuration takes to a greater extent than than 200 such determination to configure your application, If yous are interested inwards learning to a greater extent than then Spring Framework Master Class - Beginner to Expert is a practiced class to acquire Spring Boot inwards depth.

 at that topographic point is a subtle departure betwixt them Difference betwixt @SpringBootApplication vs @EnableAutoConfiguration annotations inwards Spring Boot




2. Difference betwixt @EnableAutoConfiguration together with @SpringBootApplication

Now that nosotros know what is @SpringBootApplication together with @EnableAutoConfiguration annotation together with what do they do, it's fourth dimension to highlight to a greater extent than or less of import departure betwixt them.

Here is a duet of worth-noting departure betwixt @SpringBootApplication together with @EnableAutoConfiguration annotations of Spring Boot:

1. Availability
The @SpringBootApplicaiton is relatively novel than @EnableAutoConfiguration. It was introduced inwards Spring Boot 1.2 release land @EnableAutoConfiguation is nowadays cast the Spring Boot 1.0 release.


2. Purpose
The clear exercise of @EnableAutoConfiguration is to enable automatic configuration characteristic of Spring Boot application which automatically configures things if sure enough classes are nowadays inwards Classpath e.g. it tin configure Thymeleaf TemplateResolver together with ViewResolver if Thymeleaf is nowadays inwards the classpath.

On the other hand, @SpringBootApplication does iii things, it allows yous to run the Main cast equally a JAR amongst an embedded container. It enables Java configuration together with it too enables Component Scanning.


3. Uses
It's not mandatory to lay @SpringBootApplication to create a Spring Boot application, yous tin nonetheless exercise @Configuration together with @EnableAutoConfiguration individually equally shown inwards the illustration given inwards side past times side point.


4. Control
The @EnableAutoConfiguration annotation allows yous to selectively exclude sure enough classes from auto-configuration using exclude attribute equally shown below:

@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration {   //.. Java code }

If the cast is non on the classpath, yous tin exercise the excludeName attribute of the @EnableAutoConfiguration annotation together with specify the fully qualified cast name.

If yous desire to acquire to a greater extent than nearly this annotation, I propose yous reading Craig Walls' (author of Spring inwards Action) to a greater extent than or less other masterpiece Spring Boot inwards Action.

 at that topographic point is a subtle departure betwixt them Difference betwixt @SpringBootApplication vs @EnableAutoConfiguration annotations inwards Spring Boot



3. Important points

Now that yous empathise the departure betwixt @EnableAutoConfiguration together with @SpringBootApplication, it's fourth dimension to revise to a greater extent than or less of import points nearly these 2 of import annotations.

1. You should annotate the Main cast or Bootstrap cast amongst the @SpringBootApplication, this volition permit yous to run equally a JAR amongst embedded spider web server Tomcat. If yous desire yous tin alter that to Jetty or Undertow.


2. The @SpringBootApplication is a combination of iii annotations @Configuration (used for Java-based configuration), @ComponentScan (used for element scanning), together with @EnableAutoConfiguration (used to enable auto-configuration inwards Spring Boot).


3. The @EnableAutoConfiguration annotations enable auto-configuration features of Spring Boot which configures modules based on the presence of sure enough classes on the classpath. For example, if Thymeleaf JAR is nowadays inwards classpath together with Spring MVC is enabled e.g. using spring-boot-web-starter parcel thence it tin automatically configure template resolver together with sentiment resolver for you.


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


5. In instance of auto-configuration, manually declared beans tin override beans automatically created past times auto-configuration feature. This is achieved past times using @ConditionalOnMissingBean of Spring 4.0


6. If yous are using @EnableAutoConfiguration classes thence yous tin selectively exclude sure enough classes from auto-configuration past times using exclude equally shown below:

@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)


7. The @SpringBootApplication annotation too provides aliases to customize the attributes of @EnableAutoConfiguration together with @ComponentScan annotations.


That's all nearly the departure betwixt @SpringBootApplication together with @EnableAutoConfiguration annotations of Spring Boot. As yous learned, the @SpringBootApplication makes it tardily to enable auto-configuration together with create a Bootstrap cast past times reducing the release of annotations yous commonly demand i.e. instead of 3 annotations yous simply demand one. But, if yous demand to a greater extent than command over autoconfiguration thence yous should exercise @EnableAutoConfiguration, which allows yous to exclude classes from auto-configuration.


Further Learning
Spring Framework Master Class - Beginner to Expert
Master Microservices amongst Spring Boot together with Spring Cloud
Master Hibernate together with JPA amongst Spring Boot inwards 100 Steps
Spring Boot Reference Guide
REST amongst Spring MasterClass past times Eugen Parsaschiv
Spring Boot: Efficient Development, Configuration, together with Deployment

Thanks for reading this article thence far. If yous similar this article together with my explains of departure betwixt @SpringBootApplication together with @EnableAutoConfiguration thence delight portion amongst your friends together with colleagues. If yous conduct maintain whatsoever questions or feedback thence delight drib a note.

0 Response to "Difference Betwixt @Springbootapplication Vs @Enableautoconfiguration Annotations Inward Jump Boot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel