SoFunction
Updated on 2025-04-12

The difference and description of SpringBoot project and files

Differences from files

andThe files are YAML or Properties files used in Spring Boot projects to configure the application, but they areLoading order, purpose, and priorityThere are key differences on this.

Core differences overview:

characteristic / /
Loading time Earlier (Bootstrap Context) Late (Application Context)
use Bootstrapping, external configuration Common application configuration
Context Bootstrap Context Application Context
Priority Higher (in Bootstrap Context) Lower (in Application Context)
Configuration source External configuration (Config Server, Nacos, etc.) Apply its own configuration (local file, command line, environment variable)
Applicable scenarios Configuration center connection, shared configuration, application name, profile Application common configuration, bean definition, business logic configuration
Configuration override cover Overwrite command line parameters, environment variables, etc.
Default location src/main/resources/ src/main/resources/
Whether it must exist Optional Optional (but usually present)

Detailed explanation and difference:

1. Loading Order

  • / (Bootstrap Context):Started in Spring Boot applicationVery earlyPhase loading. This is throughBootstrap ContextImplemented, it is a special parent Spring context created before the main Application Context.
  • / (Application Context):existAfter loading,When Application Context is initializedload. This is the main context of the Spring Boot application.

Understand Bootstrap Context and Application Context:

You can understand Bootstrap Context as being started by Spring BootGuidance phase, it is responsible for initializing someBasic and criticalConfiguration, for example:

  • Configure the connection information for Spring Cloud Config Client or Nacos Config Client:Bootstrap Context You need to know how to connect to the Configuration Center before you can load the external configuration.
  • Loading the shared configuration:Configurations shared between multiple applications usually need to be loaded in the Bootstrap Context to be available in the early stages of application startup.
  • set upandBasic attributes such as:These properties usually need to be determined in the Bootstrap Context so that subsequent configuration loading and application initialization can be done correctly.

Application Context is the Spring Boot applicationMain application context, responsible for loading and managing all beans in the application, processing requests, executing business logic, etc.Mainly used to configure the application itself.

2. Purpose

/ : Bootstrapping and externalized configuration:

  • Config the Spring Cloud Config Client or Nacos Config Client:For exampleor-addretc.
  • Loading shared configuration (sharedConfigs):Using Spring Cloud Alibaba Nacos ConfigsharedConfigsWhen functioning, it is necessary toConfigure in.
  • Configure the application name ():Usually inSet in the middle to identify applications, facilitate the identification of the configuration center.
  • Activate Profile ():existActivate Profile in the , which can affect subsequent configuration loading.
  • Other configurations that need to be loaded early in the application startup:For example, some custom Bootstrap listeners or initializers require configuration.

/ : Application common configuration:

  • Common configuration of the application itself:For example, port number, data source connection information (if not shared), log level, international configuration, Thymeleaf template configuration, Spring MVC configuration, Bean definition, etc.
  • Business logic-related configuration:For example, some service switches, parameter configurations, etc.
  • Most applications should be configuredmiddle.

3. Context

  • / : Bootstrap Context
  • / : Application Context

This meansThe configuration in the Bootstrap Context is mainly used, andThe configuration in the application context. The configuration of the Bootstrap Context affects the creation and initialization of the Application Context.

4. Priority

  • / Higher priority (in Bootstrap Context):The configuration of the Bootstrap Context is loaded first and affects the creation of the Application Context.
  • / Lower priority (in Application Context):The configuration of the Application Context is loaded after the Bootstrap Context.

5. Configuration Sources

  • / : Positive to configurationExternal configuration sources**, such as Spring Cloud Config Server, Nacos Config Server, etc. It is also used to configure some that need to be determined during the boot stageBasic properties
  • / : Mainly used for configurationThe application's own** configuration usually comes from the local file system, command line parameters, environment variables, etc.

6. Applicable scenarios (Use Cases)

/ : Applicable to:

  • Configure Spring Cloud Config or Nacos Config Client connection information.
  • ConfigurationsharedConfigs (Nacos Config)。
  • set upand
  • Configure properties that need to be used in the Bootstrap Context.

/ : Applicable to:

  • Configure common settings for the application.
  • Define Bean.
  • Configure business logic-related parameters.
  • Configuration of most applications.

7. Configuration Overriding

The configuration in themiddleConfiguration of the same attributes. This is becauseexistLoad afterwards.

Command line parameters, environment variables, etc.Can be further coveredandconfiguration in. The configuration priority order of Spring Boot is:

  1. Command line parameters
  2. Environment variables
  3. / (profile-specific and default)
  4. / (profile-specific and default)
  5. Default configuration (default value inside Spring Boot framework)

8. Default Location

  • andThe default locations aresrc/main/resources/In the directory.

9. Required or Optional

  • andNonemustExist. The Spring Boot app can also be started without these configuration files, and it uses the default configuration.
  • However, in actual development, in order to perform various custom configurations,(or) UsuallyRequiredExist.(or) Only when you need to use external configuration management such as Spring Cloud Config or Nacos ConfigCommonly used

Summarize

  • If you don't need external configuration management like Spring Cloud Config or Nacos Config, then you may just need(or) document. Most applications are configured inJust be medium.
  • If you need to use external configuration management such as Spring Cloud Config or Nacos Config, or you need to load some key configurations (such as shared configurations, application names, profiles, etc.) in the early stages of application startup, then you need to use(or) document.It is mainly used for configuration boot stage configuration, as well as external configuration connection information and sharing configuration.

Simple memory tips:

  • Bootstrap first, Application follow-up: Load first,After loading.
  • Bootstrap, Application application: Responsible for booting,Responsible for application configuration.
  • External Bootstrap, Application itself: Configure external configuration,Configure your own application.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.