| 此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Boot 3.5.5! | 
提前处理
Spring AOT 是一个在构建时分析应用程序并生成其优化版本的过程。
这是运行 Spring 的强制性步骤ApplicationContext在本机图像中。
| 有关 Spring Boot 中 GraalVM 本机映像支持的概述,请查看参考文档。 | 
Spring Boot Maven 插件提供了可用于对应用程序和测试代码执行 AOT 处理的目标。
处理应用程序
要将应用程序配置为使用此功能,请为process-aot目标,如以下示例所示:
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-aot</id>
			<goals>
				<goal>process-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>作为BeanFactory在构建时做好充分准备,还评估条件。
与常规 Spring Boot 应用程序在运行时执行的作相比,这有一个重要的区别。
例如,如果要选择加入或选择退出某些功能,则需要配置构建时使用的环境才能执行此作。
这process-aot因此,目标与运行目标共享许多属性。
使用本机配置文件
如果您使用spring-boot-starter-parent作为parent项目的nativeprofile 可用于简化构建本机映像所需的步骤。
这nativeprofile 配置以下内容:
- 
执行 process-aot当 Spring Boot Maven 插件应用于项目时。
- 
合适的设置,以便构建映像生成原生映像。 
- 
本机构建工具 Maven 插件的合理默认值,特别是: - 
确保插件使用原始类路径,而不是主 jar 文件,因为它不理解我们重新打包的 jar 格式。 
- 
验证是否有合适的 GraalVM 版本可用。 
- 
下载第三方可达性元数据。 
 
- 
| 使用原始类路径意味着本机图像不知道生成的 | 
要从nativeprofile,则表示应用程序的模块应定义两个插件,如以下示例所示:
<plugin>
	<groupId>org.graalvm.buildtools</groupId>
	<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>单个项目可以使用云原生 Buildpack 或原生映像构建工具在命令行上触发原生映像的生成。
要使用nativeprofile,您可以创建native配置文件,以便调用您首选的技术。
要在package阶段,将以下内容添加到多模块项目的根 POM:
<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>build-image-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>以下示例对本机生成工具执行相同的作:
<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.graalvm.buildtools</groupId>
					<artifactId>native-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>compile-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>完成上述作后,您可以构建多模块项目并在相关子模块中生成原生镜像,如以下示例所示:
$ mvn package -Pnative| “相关”子模块是表示 Spring Boot 应用程序的模块。 此类模块必须如上所述定义本机构建工具和 Spring Boot 插件。 | 
spring-boot:process-aot
org.springframework.boot:spring-boot-maven-plugin:3.4.10-SNAPSHOT
在应用程序上调用 AOT 引擎。
必填参数
| 名称 | 类型 | 默认值 | 
|---|---|---|
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | 
参数详情
classesDirectory
包含应打包到存档中的类和资源文件的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,请确保在引号之间包装多个值。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
excludes
要排除的工件定义的集合。这Exclude元素定义了强制性的groupId和artifactId组件和可选的classifier元件。配置为属性时,值应使用冒号分隔的组件以逗号分隔:groupId:artifactId,groupId:artifactId:classifier
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
generatedClasses
包含生成类的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
generatedResources
包含生成资源的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
generatedSources
包含生成源的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
includes
要包含的工件定义集合。这Include元素定义了强制性的groupId和artifactId组件和可选的classifier元件。配置为属性时,值应使用冒号分隔的组件以逗号分隔:groupId:artifactId,groupId:artifactId:classifier
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,确保在引号之间包装多个值。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
处理测试
AOT 引擎可以应用于使用 Spring 的测试上下文框架的 JUnit 5 测试。
AOT 引擎处理合适的测试,以生成ApplicationContextInitializer法典。
要将应用程序配置为使用此功能,请为process-test-aot目标,如以下示例所示:
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-test-aot</id>
			<goals>
				<goal>process-test-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>| 如果您正在使用 spring-boot-starter-parent,如果启用nativeTest轮廓。 | 
与应用程序 AOT 处理一样,BeanFactory在构建时已做好充分准备。
spring-boot:process-test-aot
org.springframework.boot:spring-boot-maven-plugin:3.4.10-SNAPSHOT
在测试中调用 AOT 引擎。
必填参数
| 名称 | 类型 | 默认值 | 
|---|---|---|
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | |
| 
 | 
 | 
参数详情
classesDirectory
包含应用于运行测试的类和资源文件的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,请确保在引号之间包装多个值。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
excludes
要排除的工件定义的集合。这Exclude元素定义了强制性的groupId和artifactId组件和可选的classifier元件。配置为属性时,值应使用冒号分隔的组件以逗号分隔:groupId:artifactId,groupId:artifactId:classifier
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
generatedClasses
包含生成的测试类的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
generatedResources
包含生成的测试资源的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
generatedSources
包含生成源的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
generatedTestClasses
包含生成的测试类的目录。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | 
 | 
| 用户属性 | |
| 因为 | 
includes
要包含的工件定义集合。这Include元素定义了强制性的groupId和artifactId组件和可选的classifier元件。配置为属性时,值应使用冒号分隔的组件以逗号分隔:groupId:artifactId,groupId:artifactId:classifier
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 | 
 | 
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,确保在引号之间包装多个值。
| 名称 | 
 | 
|---|---|
| 类型 | 
 | 
| 默认值 | |
| 用户属性 | 
 | 
| 因为 |