|
此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Boot 4.0.4! |
预先处理(Ahead-of-Time Processing)
Spring AOT 是一个在构建时分析您的应用程序并生成其优化版本的过程。
这是在原生镜像(native image)中运行 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目标与run goal共享许多属性。
使用原生配置(Native Profile)
如果你将 spring-boot-starter-parent 用作项目的 parent,可以使用 native 配置文件来简化构建原生镜像所需的步骤。
native 配置文件会配置以下内容:
-
在项目上应用 Spring Boot Maven 插件时执行
process-aot。 -
合适的配置,以便 build-image 生成原生镜像。
-
Native Build Tools Maven 插件 的合理默认配置,特别是:
-
确保该插件使用原始的类路径,而不是主 JAR 文件,因为它无法识别我们重新打包后的 JAR 格式。
-
验证已安装合适的 GraalVM 版本。
-
下载第三方可达性元数据。
-
|
使用原始类路径意味着原生镜像(native image)无法识别生成的 |
为了充分利用 native 配置文件,代表应用程序的模块应定义两个插件,如下例所示:
<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>
单个项目可以在命令行上使用云原生构建包(Cloud Native Buildpacks)或原生镜像构建工具(Native Image Build Tools)来触发原生镜像的生成。
要在多模块项目中使用 native 配置文件,您可以对 native 配置文件进行自定义,使其调用您偏好的技术。
要在 package 阶段绑定云原生构建包(Cloud Native Buildpacks),请将以下内容添加到您的多模块项目的根 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>
下面的示例对 Native Build Tools 执行了相同的操作:
<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 应用程序的模块。 此类模块必须按照上述说明定义 Native Build Tools 和 Spring Boot 插件。 |
spring-boot:process-aot
org.springframework.boot:spring-boot-maven-plugin:4.1.0-M3
在应用程序上调用 AOT 引擎。
必需参数
| 姓名 | 类型 | 默认 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含应打包到归档文件中的类和资源文件的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(精确匹配)。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
excludes
要排除的构件定义集合。Exclude 元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当将其配置为属性时,值应以逗号分隔,各组件之间用冒号分隔:
groupId:artifactId,groupId:artifactId:classifier
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
generatedClasses
包含生成类的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
generatedResources
包含生成资源的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
generatedSources
包含生成源代码的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
includes
要包含的构件定义集合。Include 元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,各组件之间用冒号分隔:
groupId:artifactId,groupId:artifactId:classifier
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行中,请确保将多个值用引号括起来。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
处理测试
AOT 引擎可应用于使用 Spring 测试上下文框架的 JUnit 5 测试。 这些测试由 AOT 引擎处理,然后在原生镜像中执行。
当使用 maven.test.skip 属性跳过常规测试时,这些测试不会被执行。 |
与生产代码一样,spring-boot-starter-parent 定义了一个 nativeTest 配置文件,可用于简化在原生镜像中执行测试所需的步骤。
nativeTest 配置文件会配置以下内容:
-
在项目上应用 Spring Boot Maven 插件时执行
process-test-aot。 -
当在项目中应用Native Build Tools Maven 插件时,执行
https://graalvm.github.io/native-build-tools/0.11.5/maven-plugin.html。 该执行定义了合理的默认值,特别是:-
确保该插件使用原始的类路径,而不是主 JAR 文件,因为它无法识别我们重新打包后的 JAR 格式。
-
验证已安装合适的 GraalVM 版本。
-
下载第三方可达性元数据。
-
为了充分利用 nativeTest 配置文件,代表应用程序的模块应定义两个插件,如下例所示:
<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>
一旦上述配置在每个需要此功能的模块中完成,你就可以构建你的多模块项目,并在相关子模块中以原生镜像(native image)方式执行测试,如下例所示:
$ mvn test -PnativeTest
与应用程序的AOT处理一样,BeanFactory在构建时已完全准备就绪。 |
spring-boot:process-test-aot
org.springframework.boot:spring-boot-maven-plugin:4.1.0-M3
在测试中调用 AOT 引擎。
必需参数
| 姓名 | 类型 | 默认 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含用于运行测试的类和资源文件的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(精确匹配)。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
excludes
要排除的构件定义集合。Exclude 元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当将其配置为属性时,值应以逗号分隔,各组件之间用冒号分隔:
groupId:artifactId,groupId:artifactId:classifier
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
generatedClasses
包含生成的测试类的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
generatedResources
包含生成的测试资源的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
generatedSources
包含生成源代码的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
generatedTestClasses
包含生成的测试类的目录。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
includes
要包含的构件定义集合。Include 元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,各组件之间用冒号分隔:
groupId:artifactId,groupId:artifactId:classifier
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行中,请确保将多个值用引号括起来。
姓名 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
Since |