Purpose of *.class argument in SpringApplication.run(*.class, args)

Viewed 1276

In Sprint Boot, the Spring Application is initialized by the call to SpringApplication.run(*.class, args) in the main method. I was wondering what is the purpose of the *.class reference passed in run?

After looking through the source code, it's not immediately obvious to me why this is needed.

package org.java.simpleapp.simpleapp;

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

@SpringBootApplication
public class SimpleApplication {

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

}
1 Answers

By launching the Application class, we can complete a series of operations such as Spring initialization, automatic assembly, and so on. The start-up process through two entry points: @SpringBootApplication and SpringApplication.run


1.Entry method:

A static helper that can be used to run a SpringApplication from the specified source using default settings which primarySource the primary source to load, args the application arguments (usually passed from a Java main method) which results in the running ApplicationContext

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class<?>[] { primarySource }, args);
}

After calling the static run method, through a series of calls we will eventually enter the following position in the Spring Application class

As you can see, this method does two things, initializes the Spring Application class, and calls the internal public run method.


2. Spring Application initialization

Run the Spring application, creating and refreshing a new ApplicationContext with-param args the application arguments (usually passed from a Java main method) that return a running ApplicationContext

public ConfigurableApplicationContext run(String... args) {

      ...

      try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            listeners.started(context);
            callRunners(context, applicationArguments);
        }

      ...

}

The run() is responsible to manage the context, environment, listeners, applicationArguments, printedBanner as we can see in

prepareContext(context, environment, listeners, applicationArguments, printedBanner);

You might like to explore more details here Startup process source analysis

Related