提前处理
Spring AOT 是一个在构建时分析代码以生成优化版本的过程。 它最常用于帮助生成 GraalVM 原生图像。
Spring Boot Gradle 插件提供了可用于对应用和测试代码进行 AOT 处理的任务。 当应用GraalVM原生映像插件时,任务会自动配置:
-
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-注释主类,该processAotTask生成一个持久视图,显示将在运行时贡献的豆子,使豆子实例化尽可能直接。
工厂还可以通过回调进行额外的后期处理。
例如,这些配置用于生成 GraalVM 在原生映像中初始化上下文所需的反射配置。
作为豆子工厂在建造时完全准备就绪,同时也会评估条件。
这与普通 Spring Boot 应用程序在运行时的功能有重要区别。
例如,如果你想对某些功能选择加入或退出,你需要配置构建时使用的环境以实现这一点。
为此,processAot任务是JavaExec任务和可以根据需要配置环境变量、系统属性和参数。
这原生编译GraalVM 原生图像插件的任务会自动配置为使用processAot任务。
处理测试
AOT引擎可以应用于使用Spring测试上下文框架的JUnit 5测试。
合适的测试由processTestAot生成任务应用上下文初始化器法典。
与应用程序AOT处理类似,豆子工厂在构建时已完全准备好。
如同processAot这processTestAot任务是JavaExec子类和可以根据需要配置以影响处理。
这nativeTestGraalVM 原生图像插件的任务会自动配置为使用processAot和processTestAot任务。