此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.0! |
提前处理
当人们使用 Spring Boot 应用程序的预先处理时,经常会出现许多问题。 本节将解决这些问题。
条件
预先处理可优化应用程序并进行评估@Conditional
基于构建时环境的注释。配置文件是通过条件实现的,因此也会受到影响。
如果希望 bean 是基于预先优化的应用程序中的条件创建的,则必须在构建应用程序时设置环境。 在构建时提前处理时创建的 bean 始终在运行应用程序时创建,并且无法关闭。 为此,您可以设置构建应用程序时应使用的配置文件。
对于 Maven,这是通过设置profiles
的配置spring-boot-maven-plugin:process-aot
执行:
<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')
}
在运行预先优化的应用程序时,仅支持更改不影响条件的配置属性的配置文件,而不受限制。