Friday, May 25, 2018

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 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 e.g.
  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 lower Spring Boot version then you still need to use @Configuration, @CompnentScan, and @EnableAutoConfiguration if you need those features.




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 a 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 place to learn them quickly.

The @SpringBootApplication Annotation Example in Spring Boot





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

The @SpringBootApplication annotation is a combination of 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.

@Configuration annotation - Java based Spring Configuration


@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 e.g. 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 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:
  1. Spring Boot in Action
  2. Spring Framework Master Class - Beginner to Expert
  3. Creating Your First Spring Boot Application
  4. 5 Free Courses to Learn Spring and Spring Boot
  5. 10 Tips to become a better Java Developer in 2018
  6. 5 Spring Security Courses to Learn Online
  7. 3 Ways to Learn Spring Boot and Spring Cloud
  8. 5 Courses to Learn Spring Boot in 2018

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.

1 comment :

Vikas Yadav said...

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

Post a Comment