此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Boot 4.0.4spring-doc.cadn.net.cn

预先处理(Ahead-of-Time Processing)

当人们使用 Spring Boot 应用程序的提前处理(ahead-of-time processing)时,经常会遇到一些问题。 本节将解答这些问题。spring-doc.cadn.net.cn

条件

预先处理(Ahead-of-time processing)优化应用程序,并根据构建时的环境评估 @Conditional 注解。 配置档案(Profiles) 通过条件实现,因此也会受到影响。spring-doc.cadn.net.cn

如果你希望在提前优化(ahead-of-time optimized)的应用程序中根据某个条件创建 Bean,那么你必须在构建应用程序时设置好环境。 在构建时通过提前处理(ahead-of-time processing)所创建的 Bean,在应用程序运行时将始终被创建,并且无法关闭。 为此,你可以在构建应用程序时设置应使用的配置文件(profiles)。spring-doc.cadn.net.cn

对于 Maven,这通过设置 profiles 执行的 spring-boot-maven-plugin:process-aot 配置来实现:spring-doc.cadn.net.cn

<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 任务:spring-doc.cadn.net.cn

tasks.withType(org.springframework.boot.gradle.tasks.aot.ProcessAot).configureEach {
    args('--spring.profiles.active=profile-a,profile-b')
}

在运行预先优化(ahead-of-time optimized)的应用程序时,对于仅更改不影响条件判断的配置属性的配置文件(Profiles),其支持不受任何限制。spring-doc.cadn.net.cn