iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0
自我挑戰組

探索 Spring Boot Doc系列 第 19

Doc 7.1. SpringApplication

  • 分享至 

  • xImage
  •  

The SpringApplication class provides a convenient way to bootstrap a Spring application that is started from a main() method. In many situations, you can delegate to the static SpringApplication.run method, as shown in the following example:

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

@SpringBootApplication
public class MyApplication {

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

}

Class that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application:

  • Create an appropriate ApplicationContext instance (depending on your classpath)
  • Register a CommandLinePropertySource to expose command line arguments as Spring properties
  • Refresh the application context, loading all singleton beans
  • Trigger any CommandLineRunner beans
  1. 根據classpath 創建一個合適的 ApplicationContext
  2. 註冊一個 CommandLinePropertySource 將命令行參數作為Spring properties(意思是...
    將其存放在 Spring Evironment's Property,這裡的 Environment 是 spring.core.env 的一個介面)
  3. 刷新 application context , 載入所有單例的 (spring) beans
  4. 觸發任何的 CommandLineRunner Beans

#1. ApplicationContext 簡單來說就是存放管理的 Spring Bean 的容器 (Container) 。
#2. 什麼是CommandLinePropertySource?
CommandLinePropertySource is PropertySource subclass which is backed by command line arguments passed to a Java Application. (根據參考 CommandLinePropertySource’s Example)

如果單純查詢 PropertySource 的話,會發現有兩個 PropertySource ,兩個都包含於springframework 之內,但其中一個是類別寫作 PropertySource 屬於org.springframework.core.env 這一個包(原文 package),另一個是一個標籤 Annotation PropertySource 屬於org.springframework.context.annotation。

這裡回到 Class CommandLinePropertySource

Abstract base class for PropertySource implementations backed by command line arguments. The parameterized type T represents the underlying source of command line options. This may be as simple as a String array in the case of SimpleCommandLinePropertySource, or specific to a particular API such as JOpt's OptionSet in the case of JOptCommandLinePropertySource.

PropertySource 實作的抽象基底類別,透過由命令行參數支持,參數類型 T 代表 指令選項的底層資源。在 SimpleCommandLinePropertySource 的類別之中,參數類型T代表字串陣列,而在 JOptCommandLinePropertySource 的例子之中T代表特定API ,譬如 JOpt’s OptionSet 。

參考資料

{官方} Core features
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.spring-application

{官方} Class SpringApplication
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html

{官方} Class PropertySource
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

CommandLinePropertySource’s Example
https://www.logicbig.com/tutorials/spring-framework/spring-core/command-line-property-source.html


上一篇
Doc 6.8.3 Customizing the Restart Classloader
下一篇
SimpleCommandLinePropertySource
系列文
探索 Spring Boot Doc30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言