Ahead-of-time processing
当人们使用 Spring Boot 应用程序的提前处理时,通常会出现很多问题。本部分将解答这些问题。
A number of questions often arise when people use the ahead-of-time processing of Spring Boot applications. This section addresses those questions.
Conditions
提前处理优化了应用程序,并根据构建时的环境评估 {url-spring-framework-javadoc}/org/springframework/context/annotation/Conditional.html[条件]。Profiles 是通过条件来实现的,因此也会受到影响。
Ahead-of-time processing optimizes the application and evaluates {url-spring-framework-javadoc}/org/springframework/context/annotation/Conditional.html[conditions] based on the environment at build time. Profiles are implemented through conditions and are therefore affected, too.
如果你想要在提前优化的应用程序中基于条件创建 Bean,则必须在构建应用程序时设置环境。在构建时提前处理期间创建的 Bean 然后将在运行应用程序时始终创建,并且不能关闭。为此,你可以设置在构建应用程序时应使用的配置文件。
If you want beans that are created based on a condition in an ahead-of-time optimized application, you have to set up the environment when building the application. The beans which are created while ahead-of-time processing at build time are then always created when running the application and can’t be switched off. To do this, you can set the profiles which should be used when building the application.
对于 Maven,可以通过设置 spring-boot-maven-plugin:process-aot
执行的 profiles
配置来实现:
For Maven, this works by setting the profiles
configuration of the spring-boot-maven-plugin:process-aot
execution:
<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
任务:
For Gradle, you need to configure the ProcessAot
task:
tasks.withType(org.springframework.boot.gradle.tasks.aot.ProcessAot).configureEach {
args('--spring.profiles.active=profile-a,profile-b')
}
在运行提前优化的应用程序时,仅更改条件不受影响的配置属性的配置文件不受限制地支持。
Profiles which only change configuration properties that don’t influence conditions are supported without limitations when running ahead-of-time optimized applications.