提前處理
人們在使用 Spring Boot 應用的提前處理時,常常會遇到許多問題。本節將解答這些問題。
條件
提前處理會最佳化應用,並根據構建時的環境評估 @Conditional
註解。Profile 透過條件實現,因此也會受到影響。
如果你希望在提前最佳化應用中建立基於條件的 Bean,你需要在構建應用時設定環境。在構建時進行提前處理建立的 Bean 在執行應用時總是會被建立,且無法關閉。要實現這一點,你可以在構建應用時設定應使用的 profile。
對於 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')
}
只改變不影響條件的配置屬性的 Profile 在執行提前最佳化應用時不受限制地支援。