此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 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 应用程序在运行时所做的相比有一个重要的区别。 例如,如果要选择加入或退出某些功能,则需要配置构建时使用的环境。 因此,目标与运行目标共享许多属性。BeanFactoryprocess-aot

使用本机配置文件

如果用作项目,则可以使用配置文件来简化构建本机映像所需的步骤。spring-boot-starter-parentparentnative

配置文件配置以下内容: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>

单个项目可以使用云原生 BuildpackNative Image Build Tools 在命令行上触发原生映像的生成。

若要将配置文件用于多模块项目,可以创建配置文件的自定义项,以便它调用您的首选技术。nativenative

要在该阶段绑定云原生 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 引擎。

必需参数

名字 类型 违约

类目录

File

${project.build.outputDirectory}

生成类

File

${project.build.directory}/spring-aot/main/classes

生成的资源

File

${project.build.directory}/spring-aot/main/resources

生成源

File

${project.build.directory}/spring-aot/main/sources

可选参数

名字 类型 违约

参数

String[]

compiler参数

String

排除GroupIds

String

排除

List

包括

List

jvm参数

String

mainClass

String

配置 文件

String[]

boolean

false

systemProperty变量

Map

参数详细信息

arguments

AOT 处理时应考虑的应用程序参数。

名字

arguments

类型

java.lang.String[]

默认值

User 属性

因为

classesDirectory

包含应打包到归档文件中的类和资源文件的目录。

名字

classesDirectory

类型

java.io.File

默认值

${project.build.outputDirectory}

User 属性

因为

compilerArguments

应提供给 AOT 编译过程的参数。在命令行中,确保在引号之间换行多个值。

名字

compilerArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.compilerArguments

因为

excludeGroupIds

要排除的 groupId 名称的逗号分隔列表(完全匹配)。

名字

excludeGroupIds

类型

java.lang.String

默认值

User 属性

spring-boot.excludeGroupIds

因为

1.1.0

excludes

要排除的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:ExcludegroupIdartifactIdclassifiergroupId:artifactId,groupId:artifactId:classifier

名字

excludes

类型

java.util.List

默认值

User 属性

spring-boot.excludes

因为

1.1.0

generatedClasses

包含生成的类的目录。

名字

generatedClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/classes

User 属性

因为

generatedResources

包含生成的资源的目录。

名字

generatedResources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/resources

User 属性

因为

generatedSources

包含生成的源的目录。

名字

generatedSources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/sources

User 属性

因为

includes

要包含的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:IncludegroupIdartifactIdclassifiergroupId:artifactId,groupId:artifactId:classifier

名字

includes

类型

java.util.List

默认值

User 属性

spring-boot.includes

因为

1.2.0

jvmArguments

应与 AOT 进程关联的 JVM 参数。在命令行中,确保在引号之间换行多个值。

名字

jvmArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.jvmArguments

因为

mainClass

要用作 AOT 进程源的主类的名称。如果未指定,则将使用找到的第一个包含“main”方法的编译类。

名字

mainClass

类型

java.lang.String

默认值

User 属性

spring-boot.aot.main-class

因为

profiles

要考虑 AOT 处理的 Spring 配置文件。

名字

profiles

类型

java.lang.String[]

默认值

User 属性

因为

skip

跳过执行。

名字

skip

类型

boolean

默认值

false

User 属性

spring-boot.aot.skip

因为

systemPropertyVariables

要传递给 AOT 进程的 JVM 系统属性列表。

名字

systemPropertyVariables

类型

java.util.Map

默认值

User 属性

因为

名字 类型 违约

类目录

File

${project.build.outputDirectory}

生成类

File

${project.build.directory}/spring-aot/main/classes

生成的资源

File

${project.build.directory}/spring-aot/main/resources

生成源

File

${project.build.directory}/spring-aot/main/sources

名字 类型 违约

参数

String[]

compiler参数

String

排除GroupIds

String

排除

List

包括

List

jvm参数

String

mainClass

String

配置 文件

String[]

boolean

false

systemProperty变量

Map

名字

arguments

类型

java.lang.String[]

默认值

User 属性

因为

名字

classesDirectory

类型

java.io.File

默认值

${project.build.outputDirectory}

User 属性

因为

名字

compilerArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.compilerArguments

因为

名字

excludeGroupIds

类型

java.lang.String

默认值

User 属性

spring-boot.excludeGroupIds

因为

1.1.0

名字

excludes

类型

java.util.List

默认值

User 属性

spring-boot.excludes

因为

1.1.0

名字

generatedClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/classes

User 属性

因为

名字

generatedResources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/resources

User 属性

因为

名字

generatedSources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/sources

User 属性

因为

名字

includes

类型

java.util.List

默认值

User 属性

spring-boot.includes

因为

1.2.0

名字

jvmArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.jvmArguments

因为

名字

mainClass

类型

java.lang.String

默认值

User 属性

spring-boot.aot.main-class

因为

名字

profiles

类型

java.lang.String[]

默认值

User 属性

因为

名字

skip

类型

boolean

默认值

false

User 属性

spring-boot.aot.skip

因为

名字

systemPropertyVariables

类型

java.util.Map

默认值

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-parentnativeTest

与应用程序 AOT 处理一样,在构建时已做好充分准备。BeanFactory

如果使用 ,则在启用配置文件时会自动配置此执行。spring-boot-starter-parentnativeTest

spring-boot:process-test-aot

org.springframework.boot:spring-boot-maven-plugin:3.3.2-SNAPSHOT

在测试中调用 AOT 引擎。

必需参数

名字 类型 违约

类目录

File

${project.build.outputDirectory}

生成类

File

${project.build.directory}/spring-aot/main/classes

生成的资源

File

${project.build.directory}/spring-aot/test/resources

生成源

File

${project.build.directory}/spring-aot/test/sources

生成的测试类

File

${project.build.directory}/spring-aot/test/classes

testClasses目录

File

${project.build.testOutputDirectory}

可选参数

名字 类型 违约

compiler参数

String

排除GroupIds

String

排除

List

包括

List

jvm参数

String

boolean

false

systemProperty变量

Map

参数详细信息

classesDirectory

包含应用于运行测试的类和资源文件的目录。

名字

classesDirectory

类型

java.io.File

默认值

${project.build.outputDirectory}

User 属性

因为

compilerArguments

应提供给 AOT 编译过程的参数。在命令行中,确保在引号之间换行多个值。

名字

compilerArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.compilerArguments

因为

excludeGroupIds

要排除的 groupId 名称的逗号分隔列表(完全匹配)。

名字

excludeGroupIds

类型

java.lang.String

默认值

User 属性

spring-boot.excludeGroupIds

因为

1.1.0

excludes

要排除的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:ExcludegroupIdartifactIdclassifiergroupId:artifactId,groupId:artifactId:classifier

名字

excludes

类型

java.util.List

默认值

User 属性

spring-boot.excludes

因为

1.1.0

generatedClasses

包含生成的测试类的目录。

名字

generatedClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/classes

User 属性

因为

generatedResources

包含生成的测试资源的目录。

名字

generatedResources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/resources

User 属性

因为

generatedSources

包含生成的源的目录。

名字

generatedSources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/sources

User 属性

因为

generatedTestClasses

包含生成的测试类的目录。

名字

generatedTestClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/classes

User 属性

因为

includes

要包含的项目定义的集合。该元素定义必需组件和组件以及可选组件。配置为属性时,值应以逗号分隔,并用冒号分隔的组件分隔:IncludegroupIdartifactIdclassifiergroupId:artifactId,groupId:artifactId:classifier

名字

includes

类型

java.util.List

默认值

User 属性

spring-boot.includes

因为

1.2.0

jvmArguments

应与 AOT 进程关联的 JVM 参数。在命令行中,确保在引号之间换行多个值。

名字

jvmArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.jvmArguments

因为

skip

跳过执行。

名字

skip

类型

boolean

默认值

false

User 属性

spring-boot.aot.skip

因为

systemPropertyVariables

要传递给 AOT 进程的 JVM 系统属性列表。

名字

systemPropertyVariables

类型

java.util.Map

默认值

User 属性

因为

testClassesDirectory

包含应打包到归档文件中的类和资源文件的目录。

名字

testClassesDirectory

类型

java.io.File

默认值

${project.build.testOutputDirectory}

User 属性

因为

名字 类型 违约

类目录

File

${project.build.outputDirectory}

生成类

File

${project.build.directory}/spring-aot/main/classes

生成的资源

File

${project.build.directory}/spring-aot/test/resources

生成源

File

${project.build.directory}/spring-aot/test/sources

生成的测试类

File

${project.build.directory}/spring-aot/test/classes

testClasses目录

File

${project.build.testOutputDirectory}

名字 类型 违约

compiler参数

String

排除GroupIds

String

排除

List

包括

List

jvm参数

String

boolean

false

systemProperty变量

Map

名字

classesDirectory

类型

java.io.File

默认值

${project.build.outputDirectory}

User 属性

因为

名字

compilerArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.compilerArguments

因为

名字

excludeGroupIds

类型

java.lang.String

默认值

User 属性

spring-boot.excludeGroupIds

因为

1.1.0

名字

excludes

类型

java.util.List

默认值

User 属性

spring-boot.excludes

因为

1.1.0

名字

generatedClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/main/classes

User 属性

因为

名字

generatedResources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/resources

User 属性

因为

名字

generatedSources

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/sources

User 属性

因为

名字

generatedTestClasses

类型

java.io.File

默认值

${project.build.directory}/spring-aot/test/classes

User 属性

因为

名字

includes

类型

java.util.List

默认值

User 属性

spring-boot.includes

因为

1.2.0

名字

jvmArguments

类型

java.lang.String

默认值

User 属性

spring-boot.aot.jvmArguments

因为

名字

skip

类型

boolean

默认值

false

User 属性

spring-boot.aot.skip

因为

名字

systemPropertyVariables

类型

java.util.Map

默认值

User 属性

因为

名字

testClassesDirectory

类型

java.io.File

默认值

${project.build.testOutputDirectory}

User 属性

因为