The @SpringBootApplication annotation Example in Java + Spring Boot
Hello guys, today, we'll learn about the @SpringBootApplication annotation, one of the most important annotations from popular Spring Boot framework, which has changed the way Java developers use Spring framework for writing Java applications. In this article, I'll explain to you the meaning of @SpringBootApplication and it's used in a simple Spring Boot application. We use @SpringBootApplication annotation on 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.
If you have been using Spring Boot for a long time then you know that earlier we need to annotate our Application class or Main class with quite a lot of annotations to start with like
- @Configuration to enable Java-based configuration,
- @ComponentScan to enable component scanning,
- and @EnableAutoConfiguration to enable Spring Boot's auto-configuration feature,
Btw, this annotation is available from Spring 1.2 onwards which means if you are running on lower Spring Boot version then you will still need to use the @Configuration, @CompnentScan, and @EnableAutoConfiguration if you need those features.
And, if you want to learn more about changes between Spring 1.2 and Spring 2.0 or want to learn Spring Boot from scratch, you can also check out this Spring Boot For Beginners online course on Udemy, one of the best course to learn Spring Boot online. I really like the way instructor Nelson Djalo explains concepts and walks through examples.
1. The @SpringBootApplication Example
Here is a simple example of how to write a Spring Boot application using @SpringBootApplication annotation. This code example is taken from my earlier article about consuming RESTful web service using Spring. In this example, we have used RestTempalte class to consume a RESTful web service.package tool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class Hello implements CommandLineRunner { private static final Logger log = LoggerFactory.getLogger(Hello.class); public static void main(String args[]) { SpringApplication.run(Hello.class); } @Override public void run(String... args) throws Exception {
RestTemplate
restTemplate = new RestTemplate(); Country country = restTemplate.getForObject( "http://www.services.groupkt.com/country/get/iso2code/US", Country.class); log.info(country.toString()); } }
The Main class serves two purposes in a Spring Boot application: configuration and bootstrapping. First, it's the main Spring configuration class and second, it enables the auto-configuration feature of Spring Boot application.
If you are interested in learning more about essential Spring Boot features e.g. auto-configuration and Starter dependency then Spring Boot Essentials is a good course to learn them quickly.
2. @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration
The @SpringBootApplication annotation is a combination of the following three Spring annotations and provides the functionality of all three with just one line of code:@Configuration
This annotation marks a class as a Configuration class in Java-based configuration. This is particularly important if you favor Java-based configuration over XML configuration. If you are not familiar with Java Based Configuration, See Spring Framework MasterClass - Beginners to Expert to learn essential Spring concepts in depth.
@ComponentScan
This annotation enables component-scanning so that the web controller classes and other components you create will be automatically discovered and registered as beans in Spring's Application Context. All the @Controller classes you write is discovered by this annotation.
@EnableAutoConfiguration
This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.
For example, if you are writing a Spring MVC application and you have Thymeleaf JAR files on application classpath then Spring Boot auto-configuration can automatically configure Thymeleaf template resolver, view resolver, and other settings automatically.
So, you can say that @SpringBootApplication is a 3-in-1 annotation which combines the functionality of @Configuration, @ComponentScan, and @EnableAutoConfiguration.
It also marks the class as a BootStrap class which means you can runt it as a normal Java class like by running its JAR file from the command prompt as shown here, or just right click and runs a Java program in Eclipse IDE.
This will start the embedded server which comes along with Spring Boot and runs your web application inside it. Once you see the log without any error, you can go to the browser and open the localhost with server port to access your Spring Boot application.
That's all about what is @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. You can just write this one line of code i.e. @SpringBootApplicaiton annotation to enable Java-based configuration, component scanning, and to enable the auto-configuration feature of Spring Boot. It makes your code more readable.
If you want to learn more about Spring Boot, here are some of the useful resources e.g. books and courses for further reading:
- Spring Boot in Action
- Spring Framework Master Class - Beginner to Expert
- Creating Your First Spring Boot Application
- 5 Free Courses to Learn Spring and Spring Boot
- 10 Tips to become a better Java Developer in 2019
- 5 Spring Security Courses to Learn Online
- 3 Ways to Learn Spring Boot and Spring Cloud
- 5 Courses to Learn Spring Boot in 2019
- Top 5 Courses to Learn Spring Framework in Depth
That's all about reading this article so far. If you like @SpringBootApplication annotation and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are looking for a hands-on, code focuses course to learn Spring 5 and Spring Boot, then I also suggest you to take a look at the Eugen Paraschiv's Learn Spring: The Certification Class , one of the best course to learn Spring 5 and Spring Boot 2 from scratch, in a guided, code-focused way.
Join the conversation