提前處理

Spring AOT 是一個在構建時分析你的應用並生成其最佳化版本的程序。它是為了在原生映象中執行 Spring ApplicationContext 的強制步驟。

關於 Spring Boot 中 GraalVM 原生映象支援的概述,請查閱參考文件

Spring Boot Maven 外掛提供了可用於對應用程式碼和測試程式碼執行 AOT 處理的目標(goals)。

處理應用

要配置你的應用以使用此特性,請為 process-aot 目標新增一個執行配置,如下例所示

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-aot</id>
			<goals>
				<goal>process-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>

由於 BeanFactory 在構建時已完全準備好,條件也會被評估。與常規 Spring Boot 應用在執行時所做的相比,這是一個重要的區別。例如,如果你想選擇啟用或停用某些特性,你需要在構建時配置所使用的環境。因此,process-aot 目標與 run 目標共享許多屬性。

使用原生 Profile

如果你使用 spring-boot-starter-parent 作為你專案的 parent,則可以使用 native profile 來簡化構建原生映象所需的步驟。

native profile 配置了以下內容

  • Spring Boot Maven 外掛應用於專案時執行 process-aot

  • 合適的設定,以便 build-image 生成原生映象。

  • Native Build Tools Maven 外掛的合理預設設定,特別是

    • 確保外掛使用原始 classpath,而不是主 jar 檔案,因為它不理解我們重新打包的 jar 格式。

    • 驗證是否安裝了合適的 GraalVM 版本。

    • 下載第三方可達性元資料。

使用原始 classpath 意味著原生映象不知道生成的 MANIFEST.MF 檔案。如果你需要在原生映象中讀取 manifest 的內容,例如獲取應用的實現版本,請配置 classesDirectory 選項以使用常規 jar。

為了受益於 native profile,代表應用的模組應定義兩個外掛,如下例所示

<plugin>
	<groupId>org.graalvm.buildtools</groupId>
	<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

單個專案可以使用 雲原生 BuildpacksNative Image Build Tools 在命令列觸發原生映象的生成。

要在多模組專案中使用 native profile,你可以自定義 native profile,以便它呼叫你偏好的技術。

要在 package 階段繫結雲原生 Buildpacks,請將以下內容新增到你的多模組專案的根 POM 中

<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>build-image-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>

下面的示例對 Native Build Tools 也做了同樣的事情

<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.graalvm.buildtools</groupId>
					<artifactId>native-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>compile-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>

完成上述配置後,你可以構建你的多模組專案,並在相關的子模組中生成原生映象,如下例所示

$ mvn package -Pnative
"相關的" 子模組是代表 Spring Boot 應用的模組。這種模組必須定義上面描述的 Native Build Tools 和 Spring Boot 外掛。

spring-boot:process-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.5

在應用上呼叫 AOT 引擎。

必需引數

名稱 型別 預設值

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

${project.build.directory}/spring-aot/main/classes

generatedResources

File

${project.build.directory}/spring-aot/main/resources

generatedSources

File

${project.build.directory}/spring-aot/main/sources

可選引數

名稱 型別 預設值

arguments

String[]

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

mainClass

String

profiles

String[]

skip

boolean

false

systemPropertyVariables

Map

引數詳情

arguments

應該為 AOT 處理考慮的應用引數。

名稱

arguments

型別

java.lang.String[]

預設值

使用者屬性

始於

classesDirectory

包含應打包到歸檔中的類和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

始於

compilerArguments

應提供給 AOT 編譯程序的引數。在命令列中,確保用引號將多個值括起來。

名稱

compilerArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

始於

excludeGroupIds

逗號分隔的要排除的 groupId 名稱列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件之間用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

generatedClasses

包含生成的類的目錄。

名稱

generatedClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/classes

使用者屬性

始於

generatedResources

包含生成的資源的目錄。

名稱

generatedResources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/resources

使用者屬性

始於

generatedSources

包含生成的原始碼的目錄。

名稱

generatedSources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/sources

使用者屬性

始於

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件之間用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

jvmArguments

應與 AOT 程序關聯的 JVM 引數。在命令列中,確保用引號將多個值括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

始於

mainClass

用作 AOT 程序源的主要類名稱。如果未指定,將使用找到的第一個包含 'main' 方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.main-class

始於

profiles

為 AOT 處理考慮的 Spring profiles。

名稱

profiles

型別

java.lang.String[]

預設值

使用者屬性

始於

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.aot.skip

始於

systemPropertyVariables

傳遞給 AOT 程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

始於

處理測試

AOT 引擎可以應用於使用 Spring 測試上下文框架的 JUnit 5 測試。AOT 引擎會處理合適的測試,以便生成 ApplicationContextInitializer 程式碼。

要配置你的應用以使用此特性,請為 process-test-aot 目標新增一個執行配置,如下例所示

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-test-aot</id>
			<goals>
				<goal>process-test-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>
如果你使用 spring-boot-starter-parent,啟用 nativeTest profile 會自動配置此執行。

與應用 AOT 處理一樣,BeanFactory 在構建時已完全準備好。

spring-boot:process-test-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.5

在測試上呼叫 AOT 引擎。

必需引數

名稱 型別 預設值

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

${project.build.directory}/spring-aot/main/classes

generatedResources

File

${project.build.directory}/spring-aot/test/resources

generatedSources

File

${project.build.directory}/spring-aot/test/sources

generatedTestClasses

File

${project.build.directory}/spring-aot/test/classes

testClassesDirectory

File

${project.build.testOutputDirectory}

可選引數

名稱 型別 預設值

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

skip

boolean

false

systemPropertyVariables

Map

引數詳情

classesDirectory

包含應應用於執行測試的類和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

始於

compilerArguments

應提供給 AOT 編譯程序的引數。在命令列中,確保用引號將多個值括起來。

名稱

compilerArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

始於

excludeGroupIds

逗號分隔的要排除的 groupId 名稱列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件之間用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

generatedClasses

包含生成的測試類的目錄。

名稱

generatedClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/classes

使用者屬性

始於

generatedResources

包含生成的測試資源的目錄。

名稱

generatedResources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/resources

使用者屬性

始於

generatedSources

包含生成的原始碼的目錄。

名稱

generatedSources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/sources

使用者屬性

始於

generatedTestClasses

包含生成的測試類的目錄。

名稱

generatedTestClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/classes

使用者屬性

始於

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件之間用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

jvmArguments

應與 AOT 程序關聯的 JVM 引數。在命令列中,確保用引號將多個值括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

始於

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.aot.skip

始於

systemPropertyVariables

傳遞給 AOT 程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

始於

testClassesDirectory

包含應打包到歸檔中的類和資原始檔的目錄。

名稱

testClassesDirectory

型別

java.io.File

預設值

${project.build.testOutputDirectory}

使用者屬性

始於