提前处理
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 '3.5.5'
id 'org.graalvm.buildtools.native' version '0.10.6'
id 'java'
}
plugins {
id("org.springframework.boot") version "3.5.5"
id("org.graalvm.buildtools.native") version "0.10.6"
java
}
处理应用程序
基于您的@SpringBootApplication
-annotated 主类,processAot
任务生成将在运行时贡献的 Bean 的持久视图,使 Bean 实例化尽可能简单。
可以使用回调对工厂进行额外的后处理。
例如,这些用于生成 GraalVM 在原生映像中初始化上下文所需的必要反射配置。
作为BeanFactory
在构建时做好充分准备,还评估条件。
与常规 Spring Boot 应用程序在运行时执行的作相比,这有一个重要的区别。
例如,如果要选择加入或选择退出某些功能,则需要配置构建时使用的环境才能执行此作。
为此,该processAot
task 是一个JavaExec
任务,并可以根据需要配置环境变量、系统属性和参数。
这nativeCompile
任务会自动配置为使用processAot
任务。
处理测试
AOT 引擎可以应用于使用 Spring 的测试上下文框架的 JUnit 5 测试。
合适的测试由processTestAot
要生成的任务ApplicationContextInitializer
法典。
与应用程序 AOT 处理一样,BeanFactory
在构建时已做好充分准备。
与processAot
这processTestAot
任务是JavaExec
子类,可以根据需要进行配置以影响此处理。
这nativeTest
任务会自动配置为使用processAot
和processTestAot
任务。