提前处理
原文:https://docs.spring.io/spring-boot/how-to/aot.html (Spring Boot 4.2.0)
使用 Spring Boot 应用的提前处理时经常会出现一些问题。本节解决这些问题。
条件
提前处理会根据构建时的环境优化应用并评估 @Conditional 注解。Profile 通过条件实现,因此也会受到影响。
如果希望在提前优化的应用中创建基于条件的 bean,必须在构建应用时设置环境。构建时提前处理期间创建的 bean 在运行应用时始终会创建,无法关闭。为此,可以设置构建应用时应使用的 profile。
对于 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>dev</profiles> </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </profile>
对于 Gradle,可以在 AOT 任务中配置 profile。