提前处理

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://apache.ac.cn/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.