我正在构建一个库模块,作为构建 Spring 应用原生镜像的助手,例如添加 AOT bean 处理器和过滤器。我还需要在 AOT 期间设置一些 Spring 属性,然后激活一些在 AOT 期间要查找和注册的 bean 定义。
由于这是一个库,我无法控制正在构建的应用程序,因此我无法使用属性文件或环境变量来设置属性。我尝试过使用,EnvironmentPostProcessor
但看起来它在 AOT 处理期间没有被评估:
class EnableKubernetesEnvironemntPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources().addFirst(new MapPropertySource("enableKubernetes", Map.of(
"spring.main.cloud.platform", "kubernetes",
"spring.profiles.active", "kubernetes",
"management.server.port", "9000"
)));
}
}
如何在 AOT 处理期间以编程方式设置 Spring 属性?是否可以EnvironmentPostProcessor
在 AOT 期间触发?
具体来说,我需要设置spring.main.cloud.platform=kubernetes
,以便spring.profiles.active=kubernetes
AOT 选择符合这些属性条件的 bean,并且management.server.port
AOT 知道为 Actuator 使用不同的容器。