Saturday, March 18, 2023

What is @SpringBootApplication Annotation in Java and Spring Boot? Example Tutorial

Hello guys, if you are wondering what is @SpringBootApplicaiton annotation and how to use it then you have come to the right place. Today, we'll learn about the @SpringBootApplication annotation, one of the most important annotations from the popular Spring Boot framework, which has changed the way Java developers use the Spring framework for writing Java applications. In this article, I'll explain to you the meaning of @SpringBootApplication and its use 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
  1. @Configuration to enable Java-based configuration, 
  2. @ComponentScan to enable component scanning, 
  3. and @EnableAutoConfiguration to enable Spring Boot's auto-configuration feature, 
but now you can do all that by just annotating your Application class with @SpringBootApplication.

Btw, this annotation is available from Spring 1.2 onwards which means if you are running on a 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 & Hibernate for Beginners (includes Spring Boot) online course on Udemy, one of the best courses to learn Spring Boot online. I really like the way instructor Chad Darby explains concepts and walks through examples.





1. The @SpringBootApplication Example in Java

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 the Spring Boot application.

If you are interested in learning more about essential Spring Boot features like auto-configuration and Starter dependency then Spring Boot Essentials is a good course to learn them quickly.

The @SpringBootApplication Annotation Example in Java





2. @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration

The @SpringBootApplication annotation is a combination of the @Configuration, @ComponentScan, and @EnableAutoConfiguration annotations in Spring Framework and provides the functionality of all three with just one line of code:

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

what is @springbootapplication annotation in Java


2. @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 are discovered by this annotation.


3. @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 the 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 that combines the functionality of @Configuration, @ComponentScan, and @EnableAutoConfiguration.

It also marks the class as a BootStrap class which means you can run it as a normal Java class by running its JAR file from the command prompt as shown here, or just right-click and runs a Java program in Eclipse IDE.

The @SpringBootApplication Annotation Example in Java + Spring Boot

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 a 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 like books and courses for further reading:


And, if you want to read more, here are some Spring frameworks articles you may like

That's all about reading this article so far. If you like the @SpringBootApplication annotation and my explanation then please share it 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-focused course to learn Spring 5 and Spring Boot, then I also suggest you take a look at 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.

1 comment :

Unknown said...

good explanation..pls share some more articles related spring boot and spring cloud.
Thanks

Post a Comment