对于最新的稳定版本,请使用 Spring Boot 3.5.5spring-doc.cadn.net.cn

提前处理

Spring AOT 是一个在构建时分析代码以生成代码优化版本的过程。 它最常用于帮助生成 GraalVM 本机映像。spring-doc.cadn.net.cn

Spring Boot Gradle 插件提供的任务可用于对应用程序和测试代码执行 AOT 处理。 应用 GraalVM 本机映像插件时,会自动配置这些任务:spring-doc.cadn.net.cn

/*
 * 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.4.9'
	id 'org.graalvm.buildtools.native' version '0.10.6'
	id 'java'
}
plugins {
	id("org.springframework.boot") version "3.4.9"
	id("org.graalvm.buildtools.native") version "0.10.6"
	java
}

处理应用程序

基于您的@SpringBootApplication-annotated 主类,processAot任务生成将在运行时贡献的 Bean 的持久视图,使 Bean 实例化尽可能简单。 可以使用回调对工厂进行额外的后处理。 例如,这些用于生成 GraalVM 在原生映像中初始化上下文所需的必要反射配置。spring-doc.cadn.net.cn

作为BeanFactory在构建时做好充分准备,还评估条件。 与常规 Spring Boot 应用程序在运行时执行的作相比,这有一个重要的区别。 例如,如果要选择加入或选择退出某些功能,则需要配置构建时使用的环境才能执行此作。 为此,该processAottask 是一个JavaExec任务,并可以根据需要配置环境变量、系统属性和参数。spring-doc.cadn.net.cn

nativeCompile任务会自动配置为使用processAot任务。spring-doc.cadn.net.cn

处理测试

AOT 引擎可以应用于使用 Spring 的测试上下文框架的 JUnit 5 测试。 合适的测试由processTestAot要生成的任务ApplicationContextInitializer法典。 与应用程序 AOT 处理一样,BeanFactory在构建时已做好充分准备。 与processAotprocessTestAot任务是JavaExec子类,可以根据需要进行配置以影响此处理。spring-doc.cadn.net.cn

nativeTest任务会自动配置为使用processAotprocessTestAot任务。spring-doc.cadn.net.cn