Even though both @SpringBootApplication as well as @EnableAutoConfiguration tin hold out used to enable the auto-configuration characteristic of Spring Boot in that place 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 3 annotations: @Configuration which is used inward Java-based configuration on Spring framework, @ComponentScan to enable constituent scanning of components you lot write e.g. @Controller classes, as well as @EnableAutoConfgiuration itself, which is used to enable auto-configuration inward Spring Boot application. Spring Boot designers realize that these 3 annotations are oftentimes used together hence they bundled them into @SpringBootApplicaiton. Now, instead of 3 annotations you lot simply demand to specify 1 annotation on your Main class.
What is the departure betwixt @EnableAutoConfiguration as well as @SpringBootApplication is too a mutual Spring Boot interview interrogation as well as it was late asked 1 of my friends on his Java interview. Given the popularity of Spring Boot framework, it's skillful to know such questions earlier you lot become for your side past times side Java interview.
In this article, I'll explicate what does the @SpringBootApplication as well as @EnableAutoConfiguration practise as well as highlight about of import departure betwixt them hence that you lot tin respond this interrogation alongside confidence if you lot plow over to come across them inward your interview.
The Spring Boto auto-configuration feature tries to automatically configure your Spring application based upon the JAR dependency you lot get got added inward the classpath.
For example, if HSQLDB is nowadays on your classpath as well as you lot get got non configured whatsoever database manually, Spring volition auto-configure an in-memory database for you.
By default, this auto-configuration characteristic is non enabled as well as you lot demand to opt-in for it past times adding the @EnableAutoConfiguration or @SpringBootApplicaiton annotations to 1 of your @Configuration classes, by as well as large the Main bird 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 you lot are interested inward learning to a greater extent than then Spring Framework Master Class - Beginner to Expert is a skillful course of report to larn Spring Boot inward depth.
Here is a duad of worth-noting departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration annotations of Spring Boot:
1. Availability
The @SpringBootApplicaiton is relatively novel than @EnableAutoConfiguration. It was introduced inward Spring Boot 1.2 release piece @EnableAutoConfiguation is nowadays shape the Spring Boot 1.0 release.
2. Purpose
The clear piece of work of @EnableAutoConfiguration is to enable automatic configuration characteristic of Spring Boot application which automatically configures things if sure as shooting classes are nowadays inward Classpath e.g. it tin configure Thymeleaf TemplateResolver as well as ViewResolver if Thymeleaf is nowadays inward the classpath.
On the other hand, @SpringBootApplication does 3 things, it allows you lot to run the Main bird every bit a JAR alongside an embedded container. It enables Java configuration as well as it too enables Component Scanning.
3. Uses
It's not mandatory to position @SpringBootApplication to create a Spring Boot application, you lot tin even hence occupation @Configuration as well as @EnableAutoConfiguration individually every bit shown inward the illustration given inward side past times side point.
4. Control
The @EnableAutoConfiguration annotation allows you lot to selectively exclude sure as shooting classes from auto-configuration using exclude attribute every bit shown below:
If the bird is non on the classpath, you lot tin occupation the excludeName attribute of the @EnableAutoConfiguration annotation as well as specify the fully qualified bird name.
If you lot desire to larn to a greater extent than well-nigh this annotation, I advise you lot reading Craig Walls' (author of Spring inward Action) about other masterpiece Spring Boot inward Action.
1. You should annotate the Main bird or Bootstrap bird alongside the @SpringBootApplication, this volition permit you lot to run every bit a JAR alongside embedded spider web server Tomcat. If you lot desire you lot tin alter that to Jetty or Undertow.
2. The @SpringBootApplication is a combination of 3 annotations @Configuration (used for Java-based configuration), @ComponentScan (used for constituent scanning), as well as @EnableAutoConfiguration (used to enable auto-configuration inward Spring Boot).
3. The @EnableAutoConfiguration annotations enable auto-configuration features of Spring Boot which configures modules based on the presence of sure as shooting classes on the classpath. For example, if Thymeleaf JAR is nowadays inward classpath as well as Spring MVC is enabled e.g. using spring-boot-web-starter parcel as well as hence it tin automatically configure template resolver as well as persuasion 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 you lot are using @EnableAutoConfiguration classes as well as hence you lot tin selectively exclude sure as shooting classes from auto-configuration past times using exclude every bit shown below:
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
7. The @SpringBootApplication annotation too provides aliases to customize the attributes of @EnableAutoConfiguration as well as @ComponentScan annotations.
That's all well-nigh the departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration annotations of Spring Boot. As you lot learned, the @SpringBootApplication makes it tardily to enable auto-configuration as well as create a Bootstrap bird past times reducing the position out of annotations you lot usually demand i.e. instead of 3 annotations you lot simply demand one. But, if you lot demand to a greater extent than command over autoconfiguration as well as hence you lot should occupation @EnableAutoConfiguration, which allows you lot to exclude classes from auto-configuration.
Further Learning
Spring Framework Master Class - Beginner to Expert
Master Microservices alongside Spring Boot as well as Spring Cloud
Master Hibernate as well as JPA alongside Spring Boot inward 100 Steps
Spring Boot Reference Guide
REST alongside Spring MasterClass past times Eugen Parsaschiv
Spring Boot: Efficient Development, Configuration, as well as Deployment
Thanks for reading this article hence far. If you lot similar this article as well as my explains of departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration as well as hence delight portion alongside your friends as well as colleagues. If you lot get got whatsoever questions or feedback as well as hence delight drib a note.
What is the departure betwixt @EnableAutoConfiguration as well as @SpringBootApplication is too a mutual Spring Boot interview interrogation as well as it was late asked 1 of my friends on his Java interview. Given the popularity of Spring Boot framework, it's skillful to know such questions earlier you lot become for your side past times side Java interview.
In this article, I'll explicate what does the @SpringBootApplication as well as @EnableAutoConfiguration practise as well as highlight about of import departure betwixt them hence that you lot tin respond this interrogation alongside confidence if you lot plow over to come across them inward your interview.
1. What is Auto-Configuration inward Spring Boot?
If you lot don't know, Spring Boot aims to simplify Java evolution alongside Spring framework. Currently, Spring does a lot for you lot but inward return, it too asks a lot from you lot inward damage of configuration as well as 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 lot get got added inward the classpath.
For example, if HSQLDB is nowadays on your classpath as well as you lot get got non configured whatsoever database manually, Spring volition auto-configure an in-memory database for you.
By default, this auto-configuration characteristic is non enabled as well as you lot demand to opt-in for it past times adding the @EnableAutoConfiguration or @SpringBootApplicaiton annotations to 1 of your @Configuration classes, by as well as large the Main bird 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 you lot are interested inward learning to a greater extent than then Spring Framework Master Class - Beginner to Expert is a skillful course of report to larn Spring Boot inward depth.
2. Difference betwixt @EnableAutoConfiguration as well as @SpringBootApplication
Now that nosotros know what is @SpringBootApplication as well as @EnableAutoConfiguration annotation as well as what practise they do, it's fourth dimension to highlight about of import departure betwixt them.Here is a duad of worth-noting departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration annotations of Spring Boot:
1. Availability
The @SpringBootApplicaiton is relatively novel than @EnableAutoConfiguration. It was introduced inward Spring Boot 1.2 release piece @EnableAutoConfiguation is nowadays shape the Spring Boot 1.0 release.
2. Purpose
The clear piece of work of @EnableAutoConfiguration is to enable automatic configuration characteristic of Spring Boot application which automatically configures things if sure as shooting classes are nowadays inward Classpath e.g. it tin configure Thymeleaf TemplateResolver as well as ViewResolver if Thymeleaf is nowadays inward the classpath.
On the other hand, @SpringBootApplication does 3 things, it allows you lot to run the Main bird every bit a JAR alongside an embedded container. It enables Java configuration as well as it too enables Component Scanning.
3. Uses
It's not mandatory to position @SpringBootApplication to create a Spring Boot application, you lot tin even hence occupation @Configuration as well as @EnableAutoConfiguration individually every bit shown inward the illustration given inward side past times side point.
4. Control
The @EnableAutoConfiguration annotation allows you lot to selectively exclude sure as shooting classes from auto-configuration using exclude attribute every bit shown below:
@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { //.. Java code }
If the bird is non on the classpath, you lot tin occupation the excludeName attribute of the @EnableAutoConfiguration annotation as well as specify the fully qualified bird name.
If you lot desire to larn to a greater extent than well-nigh this annotation, I advise you lot reading Craig Walls' (author of Spring inward Action) about other masterpiece Spring Boot inward Action.
3. Important points
Now that you lot sympathise the departure betwixt @EnableAutoConfiguration as well as @SpringBootApplication, it's fourth dimension to revise about of import points well-nigh these 2 of import annotations.1. You should annotate the Main bird or Bootstrap bird alongside the @SpringBootApplication, this volition permit you lot to run every bit a JAR alongside embedded spider web server Tomcat. If you lot desire you lot tin alter that to Jetty or Undertow.
2. The @SpringBootApplication is a combination of 3 annotations @Configuration (used for Java-based configuration), @ComponentScan (used for constituent scanning), as well as @EnableAutoConfiguration (used to enable auto-configuration inward Spring Boot).
3. The @EnableAutoConfiguration annotations enable auto-configuration features of Spring Boot which configures modules based on the presence of sure as shooting classes on the classpath. For example, if Thymeleaf JAR is nowadays inward classpath as well as Spring MVC is enabled e.g. using spring-boot-web-starter parcel as well as hence it tin automatically configure template resolver as well as persuasion 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 you lot are using @EnableAutoConfiguration classes as well as hence you lot tin selectively exclude sure as shooting classes from auto-configuration past times using exclude every bit shown below:
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
7. The @SpringBootApplication annotation too provides aliases to customize the attributes of @EnableAutoConfiguration as well as @ComponentScan annotations.
That's all well-nigh the departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration annotations of Spring Boot. As you lot learned, the @SpringBootApplication makes it tardily to enable auto-configuration as well as create a Bootstrap bird past times reducing the position out of annotations you lot usually demand i.e. instead of 3 annotations you lot simply demand one. But, if you lot demand to a greater extent than command over autoconfiguration as well as hence you lot should occupation @EnableAutoConfiguration, which allows you lot to exclude classes from auto-configuration.
Further Learning
Spring Framework Master Class - Beginner to Expert
Master Microservices alongside Spring Boot as well as Spring Cloud
Master Hibernate as well as JPA alongside Spring Boot inward 100 Steps
Spring Boot Reference Guide
REST alongside Spring MasterClass past times Eugen Parsaschiv
Spring Boot: Efficient Development, Configuration, as well as Deployment
Thanks for reading this article hence far. If you lot similar this article as well as my explains of departure betwixt @SpringBootApplication as well as @EnableAutoConfiguration as well as hence delight portion alongside your friends as well as colleagues. If you lot get got whatsoever questions or feedback as well as hence delight drib a note.
No comments:
Post a Comment