跳到主要内容
版本:3.5.10

Ahead-of-Time Processing

QWen Max 中英对照 Ahead-of-Time Processing

当人们使用 Spring Boot 应用程序的预先编译(ahead-of-time)处理时,常常会提出一些问题。本节将解答这些问题。

Conditions

提前处理(Ahead-of-time processing)会在构建时根据环境对应用程序进行优化,并评估 @Conditional 注解。Profiles 通过条件实现,因此也会受到影响。

如果你想在提前优化(ahead-of-time optimized)的应用程序中基于某个条件创建 Bean,就必须在构建应用程序时设置好环境。那些在构建时通过提前处理(ahead-of-time processing)所创建的 Bean,在运行应用程序时将始终被创建,无法关闭。为此,你可以在构建应用程序时指定应使用的 profiles。

对于 Maven,这可以通过设置 spring-boot-maven-plugin:process-aot 执行的 profiles 配置来实现:

<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<configuration>
<profiles>profile-a,profile-b</profiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>

对于 Gradle,你需要配置 ProcessAot 任务:

tasks.withType(org.springframework.boot.gradle.tasks.aot.ProcessAot).configureEach {
args('--spring.profiles.active=profile-a,profile-b')
}

在运行预先优化(ahead-of-time optimized)的应用程序时,仅更改不影响条件的配置属性的 Profiles 可以不受限制地使用。