提前處理

Spring AOT 是一種在構建時分析程式碼以生成最佳化版本程式碼的過程。它最常用於幫助生成 GraalVM 原生映象。

Spring Boot Gradle 外掛提供了可用於對應用程式和測試程式碼執行 AOT 處理的任務。當應用 GraalVM Native Image 外掛 時,這些任務會自動配置。

  • Groovy

  • Kotlin

/*
 * Copyright 2012-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

plugins {
	id 'org.springframework.boot' version '4.0.0'
	id 'org.graalvm.buildtools.native' version '0.11.3'
	id 'java'
}
plugins {
	id("org.springframework.boot") version "4.0.0"
	id("org.graalvm.buildtools.native") version "0.11.3"
	java
}

處理應用程式

根據你的 @SpringBootApplication 註解主類,processAot 任務生成一個在執行時將貢獻的 bean 的持久檢視,從而使 bean 例項化儘可能直接。可以使用回撥對工廠進行額外的後處理。例如,這些用於生成 GraalVM 在原生映象中初始化上下文所需的反射配置。

由於 BeanFactory 在構建時完全準備好,因此也會評估條件。這與常規 Spring Boot 應用程式在執行時所做的有重要區別。例如,如果你想選擇加入或退出某些功能,你需要配置在構建時使用的環境來執行此操作。為此,processAot 任務是一個 JavaExec 任務,可以根據需要配置環境變數、系統屬性和引數。

GraalVM Native Image 外掛的 nativeCompile 任務會自動配置為使用 processAot 任務的輸出。

處理測試

AOT 引擎可以應用於使用 Spring 測試上下文框架的 JUnit 5 測試。透過 processTestAot 任務處理合適的測試以生成 ApplicationContextInitializer 程式碼。與應用程式 AOT 處理一樣,BeanFactory 在構建時完全準備好。與 processAot 一樣,processTestAot 任務是 JavaExec 子類,可以根據需要進行配置以影響此處理。

GraalVM Native Image 外掛的 nativeTest 任務會自動配置為使用 processAotprocessTestAot 任務的輸出。

© . This site is unofficial and not affiliated with VMware.