Monday, March 27, 2023
HomeJavaWhat's @SpringBootApplication annotation in Java and Spring Boot? Instance Tutorial

What’s @SpringBootApplication annotation in Java and Spring Boot? Instance Tutorial

Whats up guys, if you’re questioning what’s @SpringBootApplicaiton annotation and the best way to use it then you’ve come to the precise place. In the present day, we’ll be taught in regards to the @SpringBootApplication annotation, probably the most essential annotations from the favored Spring Boot framework, which has modified the best way Java builders use the Spring framework for writing Java functions. On this article, I am going to clarify to you the that means of @SpringBootApplication and its use in a easy Spring Boot utility. We use @SpringBootApplication annotation on our Software or Essential class to allow a number of options e.g. Java-based Spring configuration, element scanning, and particularly for enabling Spring Boot’s auto-configuration function.

When you’ve got been utilizing Spring Boot for a very long time then you already know that earlier we have to annotate our Software class or Essential class with numerous annotations to start out with like

  1. @Configuration to allow Java-based configuration, 
  2. @ComponentScan to allow element scanning, 
  3. and @EnableAutoConfiguration to allow Spring Boot’s auto-configuration function, 

however now you are able to do all that by simply annotating your Software class with @SpringBootApplication.

Btw, this annotation is on the market from Spring 1.2 onwards which suggests if you’re operating on a decrease Spring Boot model then you’ll nonetheless want to make use of the @Configuration, @CompnentScan, and @EnableAutoConfiguration in the event you want these options.

And, if you wish to be taught extra about modifications between Spring 1.2 and Spring 2.0 or need to be taught Spring Boot from scratch, you can even take a look at this Spring & Hibernate for Rookies (consists of Spring Boot) on-line course on Udemy, top-of-the-line programs to be taught Spring Boot on-line. I actually like the best way teacher Chad Darby explains ideas and walks by means of examples.

1. The @SpringBootApplication Instance in Java

Right here is an easy instance of the best way to write a Spring Boot utility utilizing @SpringBootApplication annotation. This code instance is taken from my earlier article about consuming RESTful net service utilizing Spring. On this instance, now we have used RestTempalte class to eat a RESTful net service.

package deal software;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Whats up implements CommandLineRunner {

  personal static closing Logger log = LoggerFactory.getLogger(Whats up.class);

  public static void major(String args[]) { up.class);

  public void run(String... args) throws Exception {

    RestTemplate restTemplate = new RestTemplate();

    Nation nation = restTemplate.getForObject(



The Essential class serves two functions in a Spring Boot utility: configuration and bootstrapping. First, it is the principle Spring configuration class and second, it permits the auto-configuration function of the Spring Boot utility.

In case you are desirous about studying extra about important Spring Boot options like auto-configuration and Starter dependency then Spring Boot Necessities is an efficient course to be taught them shortly.

The @SpringBootApplication Annotation Example in Java

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

The @SpringBootApplication annotation is a mix of the @Configuration, @ComponentScan, and @EnableAutoConfiguration annotations in Spring Framework and supplies the performance of all three with only one line of code:

1. @Configuration

This annotation marks a category as a Configuration class in Java-based configuration. That is significantly essential in the event you favor Java-based configuration over XML configuration.  In case you are not conversant in Java Based mostly Configuration, See Spring Framework MasterClass – Rookies to Skilled to be taught important Spring ideas in depth.

what is @springbootapplication annotation in Java

2. @ComponentScan

This annotation permits element scanning in order that the online controller courses and different parts you create might be mechanically found and registered as beans in Spring’s Software Context. All of the @Controller courses you write are found by this annotation.

3. @EnableAutoConfiguration

This annotation permits the magical auto-configuration function of Spring Boot, which may mechanically configure lots of stuff for you.

For instance, if you’re writing a Spring MVC utility and you’ve got Thymeleaf JAR information on the applying classpath then Spring Boot auto-configuration can mechanically configure Thymeleaf template resolver, view resolver, and different settings mechanically.

So, you may say that @SpringBootApplication is a 3-in-1 annotation that mixes the performance of @Configuration, @ComponentScan, and @EnableAutoConfiguration.

It additionally marks the category as a BootStrap class which suggests you may run it as a standard Java class by operating its JAR file from the command immediate as proven right here, or simply right-click and runs a Java program in Eclipse IDE.

The @SpringBootApplication Annotation Example in Java + Spring Boot

It will begin the embedded server which comes together with Spring Boot and runs your net utility inside it. When you see the log with none error, you may go to the browser and open the localhost with a server port to entry your Spring Boot utility.

That is all about what’s @SpringBootApplication annotation and a easy utility to reveal the best way to use it. As I stated, this good little annotation packs numerous punch. You possibly can simply write this one line of code i.e. @SpringBootApplicaiton annotation to allow Java-based configuration, element scanning, and to allow the auto-configuration function of Spring Boot. It makes your code extra readable.

If you wish to be taught extra about Spring Boot, listed here are among the helpful sources like books and programs for additional studying:

Additional Studying
Spring Boot in Motion
Spring Framework Grasp Class – Newbie to Skilled
Creating Your First Spring Boot Software

And, if you wish to learn extra, listed here are some Spring frameworks articles you could like

That is all about studying this text to this point. In case you just like the @SpringBootApplication annotation and my rationalization then please share it with your mates and colleagues. When you’ve got any questions or suggestions then please drop a observe.

P. S. – In case you are on the lookout for a hands-on, code-focused course to be taught Spring 5 and Spring Boot, then I additionally counsel you check out Eugen Paraschiv’s Study Spring: The Certification Class, top-of-the-line course to be taught Spring 5 and Spring Boot 2 from scratch, in a guided, code-focused method.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments