@SpringBootApplication in Spring Boot
Hello guys! Today, we’ll learn about one of the most important annotations from the popular Spring Boot framework, which has changed the way Java developers use Spring for writing Java applications. In this article, I’ll explain about @SpringBootApplication and every possible questions which hits your mind…
What does it do?
— -It initializes spring framework as well as it initializes spring boot.
Why we use ?
— We use the @SpringBootApplication annotation in our Application or Main class to enable a host of features, e.g. Java-based Spring configuration, component scanning, and in particular for enabling Spring Boot’s auto-configuration feature.
Previously in Spring Boot , we need to annotate our Application class or Main class with quite a lot of annotations to start with, for example:
- @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
- @ComponentScan: enable @Component scan on the package where the application is located
- @Configuration: allow to register extra beans in the context or import additional configuration classes
A single @SpringBootApplication annotation can be used to enable the above three features.
By the way, this annotation is available from Spring 1.2 onwards, which means that if you are running on lower Spring Boot versions, then you still need to use @Configuration, @CompnentScan, and @EnableAutoConfiguration if you need those features.
So, here comes the main question,
What is @SpringBootApplication?
- @SpringBootApplication internally is a combination of the following 3 annotations.So, we can say that @SpringBootApplication is a 3-in-1 annotation that combines the functionality of @Configuration, @ComponentScan, and @EnableAutoConfiguration.
What is the alternative if we don’t want to use @SpringBootApplication annotation?
— If we don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines that behaviour so you can also use that instead.
Locating the Main Application Class
•We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @SpringBootApplication annotated class is used to search for @Entity items. Using a root package also allows component scan to apply only on your project.
•The following listing shows a typical layout:
Here is a simple example of how to write a Spring Boot application using the @SpringBootApplication annotation.
The Application.java file would declare the main method, along with the basic @SpringBootApplication, as follows:
•That’s all about the @SpringBootApplication annotation and a simple application to demonstrate how to use it. As I said, this nice little annotation packs quite a lot of punch. We can just write this one line of code to enable Java-based configuration, component scanning, and to enable the auto-configuration feature of Spring Boot. It makes your code more readable.