提前处理
人们在使用 Spring Boot 应用程序的提前处理(ahead-of-time)时,常常会遇到一些问题。 本节将解答这些问题。
条件
提前处理(Ahead-of-time processing)会优化应用程序,并在构建时根据环境评估 @Conditional 注解。
配置文件(Profiles) 是通过条件实现的,因此也会受到影响。
如果希望根据预编译优化应用程序中的条件来创建 Bean,则必须在构建应用程序时设置环境。 在构建时通过预编译处理所创建的 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)的应用程序时,对于仅更改不影响条件的配置属性的 Profile,支持没有任何限制。