跳到主要内容

预编译处理

DeepSeek V3 中英对照 Ahead-of-Time Processing

在使用 Spring Boot 应用的**提前编译(Ahead-of-Time, AOT)**处理时,常常会出现一些问题。本节将解答这些常见问题。

条件

预先处理优化了应用程序,并在构建时根据环境评估 @Conditional 注解。Profiles 通过条件实现,因此也会受到影响。

如果你希望在一个提前优化(ahead-of-time optimized)的应用程序中根据条件创建 beans,你必须在构建应用程序时设置环境。在构建时,提前处理过程中创建的 beans 在运行应用程序时将始终被创建,并且无法关闭。为此,你可以设置在构建应用程序时应使用的 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>
xml

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

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

在运行预先优化的应用程序时,仅更改不影响配置属性的配置文件支持不受限制。