此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.3.1Spring中文文档

当人们使用 Spring Boot 应用程序的提前处理时,经常会出现许多问题。 本节将解决这些问题。Spring中文文档

条件

提前处理可优化应用程序,并根据构建时的环境评估条件配置文件是通过条件实现的,因此也会受到影响。Spring中文文档

如果要根据提前优化的应用程序中的条件创建的 Bean,则必须在构建应用程序时设置环境。 在构建时提前处理时创建的 Bean 始终在运行应用程序时创建,并且无法关闭。 为此,您可以设置构建应用程序时应使用的配置文件。Spring中文文档

对于 Maven,这通过设置执行配置来工作:profilesspring-boot-maven-plugin:process-aotSpring中文文档

<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,您需要配置以下任务:ProcessAotSpring中文文档

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

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