提前处理
人们在使用 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>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')
}
只改变不影响条件的配置属性的 Profile 在运行提前优化应用时不受限制地支持。