Home > Software design >  At what stage is @SpringBootApplication executed in spring boot application?
At what stage is @SpringBootApplication executed in spring boot application?

Time:12-06

When writing a Spring Boot application, we have code like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Which means, it can run as a standalone application(having main method) - so far, so good.

My doubt starts here:

The main would be invoked by JVM, so it will see main method, and then execute the line:

 SpringApplication.run(Application.class, args);

Then, who does check the annotation @SpringBootApplication - is it interpreted on-the-fly once control goes into Spring framework when JVM executes the first line?

CodePudding user response:

This took me a while to figure out as well...

Your class Application is the 'main' class. It is calling a static method on SpringApplication.

It is the SpringApplication class that actually starts the entire Spring Boot process and all the code that will check for the Spring Annotations and things like that.

I have never looked at the code behind that class, but it has to be tremendous.

  • Related