預先處理

當人們使用 Spring Boot 應用程式的預先處理時,經常會出現許多問題。本節將解答這些問題。

條件

預先處理最佳化應用程式,並在建置時期根據環境評估 @Conditional 註解。設定檔 也透過條件實作,因此也受到影響。

如果您想要在預先處理最佳化的應用程式中,根據條件建立 Bean,您必須在建置應用程式時設定環境。在建置時期的預先處理期間建立的 Bean,在執行應用程式時將始終建立,且無法關閉。若要執行此操作,您可以設定在建置應用程式時應使用的設定檔。

對於 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')
}

僅變更不影響條件的組態屬性的設定檔,在執行預先處理最佳化的應用程式時,可以不受限制地支援。