提前处理

当人们使用 Spring Boot 应用程序的预编译处理时,经常会出现一些问题。本节将解答这些问题。

条件

预编译处理会优化应用程序并根据构建时的环境评估 @Conditional 注解。配置文件通过条件实现,因此也会受到影响。

如果希望在预编译优化的应用程序中根据条件创建 bean,则必须在构建应用程序时设置环境。在构建时进行预编译处理时创建的 bean 在运行应用程序时将始终创建,并且无法关闭。为此,可以设置在构建应用程序时应使用的配置文件。

对于 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')
}

当运行预编译优化的应用程序时,仅更改不影响条件的配置属性的配置文件不受限制地支持。

© . This site is unofficial and not affiliated with VMware.