AOT处理

当人们使用 Spring Boot 应用程序的AOT(Ahead-of-Time Processing)时,经常会遇到一些问题。 本节将解答这些问题。

条件(Conditions)

提前处理会优化应用程序,并在构建时根据环境评估 @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')
}

在运行提前优化的应用程序时,仅更改不影响条件的配置属性的 profiles 不受限制。