提前處理

當人們使用 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')
}

當執行預編譯最佳化的應用程式時,僅更改不影響條件的配置屬性的配置檔案不受限制地支援。

© . This site is unofficial and not affiliated with VMware.