Home > Java > javaTutorial > What is the SpringBoot startup process?

What is the SpringBoot startup process?

PHPz
Release: 2023-05-21 23:14:54
forward
8193 people have browsed it

Introduction to the SpringBoot startup process

The startup process of the SpringBoot application can be divided into the following steps:

  • Loading the application context

  • Scan all components in the application

  • Automatically configure the application environment

  • Start the embedded web server

Load application context

A container that contains all the components of a SpringBoot application is its context. During the startup process, SpringBoot will load and initialize this container.

The source code of this step is in the SpringApplication class. Specifically, the run method of the SpringApplication class is the entry point of this process. In this method, Spring Boot will create the application context by calling the createApplicationContext method.

The following is the source code of the createApplicationContext method:

protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            switch (this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
            }
        }
        catch (ClassNotFoundException ex) {
            throw new IllegalStateException(
                    "Unable to create a default ApplicationContext, " +
                    "please specify an ApplicationContextClass", ex);
        }
    }
    return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
Copy after login

In this method, SpringBoot will select the appropriate context class based on the application type (Servlet or Reactive). Next, use the Java reflection mechanism to instantiate the class and return a configurable application context object.

Scan all components in the application

In the previous step, SpringBoot created the application context. At this stage, SpringBoot scans all components in the application and registers them with the application context.

The source code of this step is in the scan method in the SpringApplication class. Specifically, in this method, SpringBoot creates a SpringBootBeanDefinitionScanner object and uses it to scan all components in the application.

The following is the source code of the scan method:

private void scan(String... basePackages) {
    if (ObjectUtils.isEmpty(basePackages)) {
        return;
    }
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            this.includeFilters, this.excludeFilters, this.resourceLoader);
    scanner.setResourceLoader(this.resourceLoader);
    scanner.setEnvironment(this.environment);
    scanner.setIncludeAnnotationConfig(this.useAnnotatedConfig);
    scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
        @Override
        protected boolean matchClassName(String className) {
            return getExcludeClassNames().contains(className);
        }
    });
    for (String basePackage : basePackages) {
        scanner.findCandidateComponents(basePackage).forEach(this.componentDefinitions::add);
    }
}
Copy after login

In this method, SpringBoot will create a ClassPathScanningCandidateComponentProvider object and use it to scan All components in the application. This object scans all classes under the specified package path and converts them into Spring bean definitions. These bean definitions will be registered with the application context.

Automatically configure the application environment

SpringBoot registers all components in the application into the application context in the previous step. SpringBoot automatically configures the application environment, including data sources, transaction managers, and JPA configuration.

The source code of this step is in the configureEnvironment method in the SpringApplication class. In this method, Spring Boot creates a SpringApplicationRunListeners object and uses it to configure the application environment.

The following is the source code of the configureEnvironment method:

private void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
    if (this.addCommandLineProperties) {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        environment.getPropertySources().addLast(new CommandLinePropertySource(applicationArguments));
    }
    this.listeners.environmentPrepared(environment);
    if (this.logStartupInfo) {
        this.logStartupInfo(environment);
    }
    ConfigurationPropertySources.attach(environment);
    Binder.get(environment).bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(this.sources));
    if (!this.isCustomEnvironment) {
        EnvironmentConverter.configureEnvironment(environment, this.deduceEnvironmentClass());
    }
    this.listeners.environmentPrepared(environment);
}
Copy after login

In this method, SpringBoot will create an ApplicationArguments object and convert it to A source of command line properties. It then calls the environmentPrepared method in listeners to notify the application that the environment is ready. Subsequently, SpringBoot will bind the property source to the application environment and call the environmentPrepared method in listeners to notify the application that the environment is ready.

Start the embedded web server

In the previous step, SpringBoot has automatically completed the configuration of the application environment. In this step, SpringBoot will start the embedded web server so that the application can provide web services.

The source code of this step is in the run method in the SpringApplication class. Specifically, in this method, SpringBoot selects an appropriate embedded web server based on the application type (Servlet or Reactive) and uses it to start the application.

The following is the source code of the run method:

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    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);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }
    try {
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}
Copy after login

In this method, SpringBoot will use a StopWatch object to calculate the application startup time . It then calls the starting method in listeners to notify the application that it is about to start. Next, SpringBoot prepares the application environment and uses it to create the application context. Subsequently, SpringBoot will call the started method in listeners to notify that the application has been started. Finally, SpringBoot will call the callRunners method to run all CommandLineRunner and ApplicationRunner components.

The above is the detailed content of What is the SpringBoot startup process?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template