此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1! |
Spring AOT 是一个在构建时分析应用程序并生成其优化版本的过程。
在本机映像中运行 Spring 是强制性步骤。ApplicationContext
有关 Spring Boot 中 GraalVM 本机映像支持的概述,请查看参考文档。 |
Spring Boot Maven 插件提供了可用于对应用程序和测试代码执行 AOT 处理的目标。
有关 Spring Boot 中 GraalVM 本机映像支持的概述,请查看参考文档。 |
处理应用程序
若要将应用程序配置为使用此功能,请为目标添加执行,如以下示例所示: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>
由于在构建时已做好充分准备,因此还评估了条件。
这与常规 Spring Boot 应用程序在运行时所做的相比有一个重要的区别。
例如,如果要选择加入或退出某些功能,则需要配置构建时使用的环境。
因此,目标与运行目标共享许多属性。BeanFactory
process-aot
使用本机配置文件
如果用作项目,则可以使用配置文件来简化构建本机映像所需的步骤。spring-boot-starter-parent
parent
native
配置文件配置以下内容:native
-
在项目上应用Spring Boot Maven插件时的执行。
process-aot
-
合适的设置,以便 build-image 生成本机映像。
-
Native Build Tools Maven 插件的合理默认值,特别是:
-
确保插件使用原始类路径,而不是主 jar 文件,因为它不理解我们重新打包的 jar 格式。
-
验证是否有合适的 GraalVM 版本可用。
-
下载第三方可访问性元数据。
-
若要从配置文件中获益,表示应用程序的模块应定义两个插件,如以下示例所示: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>
单个项目可以使用云原生 Buildpack 或 Native Image Build Tools 在命令行上触发原生映像的生成。
若要将配置文件用于多模块项目,可以创建配置文件的自定义项,以便它调用您的首选技术。native
native
要在该阶段绑定云原生 Buildpack,请将以下内容添加到多模块项目的根 POM:package
<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 应用程序的模块。 此类模块必须如上所述定义本机构建工具和 Spring Boot 插件。 |
spring-boot:process-aot
org.springframework.boot:spring-boot-maven-plugin:3.3.2-SNAPSHOT
在应用程序上调用 AOT 引擎。
必需参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
参数详细信息
classesDirectory
包含应打包到归档文件中的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,确保在引号之间换行多个值。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Exclude
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
generatedClasses
包含生成的类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
generatedResources
包含生成的资源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
generatedSources
包含生成的源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
includes
要包含的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Include
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行中,确保在引号之间换行多个值。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
名字 | 类型 | 违约 |
---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
加工测试
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-starter-parent nativeTest |
spring-boot:process-test-aot
org.springframework.boot:spring-boot-maven-plugin:3.3.2-SNAPSHOT
在测试中调用 AOT 引擎。
必需参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数详细信息
classesDirectory
包含应用于运行测试的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,确保在引号之间换行多个值。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Exclude
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
generatedClasses
包含生成的测试类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
generatedResources
包含生成的测试资源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
generatedSources
包含生成的源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
generatedTestClasses
包含生成的测试类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
includes
要包含的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:Include
groupId
artifactId
classifier
groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行中,确保在引号之间换行多个值。
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
名字 | 类型 | 违约 |
---|---|---|
|
||
|
||
|
||
|
||
|
||
|
|
|
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
|
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |
名字 |
|
---|---|
类型 |
|
默认值 |
|
User 属性 |
|
因为 |