如何获取所有 Spring 个个人资料名称?

How to get all Spring profile names?

我想知道是否有任何编程方式可以获取 所有 Spring 配置文件名称,活动和非活动?

有几个问题只需要活动配置文件的名称。此类问题没有回答我的问题,因为我还需要非活动配置文件的名称。

原因:我想在下拉列表中显示所有此类配置文件名称,用户可以从中select使用哪个配置文件。

没有这样的东西。

当 Spring 初始化一个 ApplicationContext 时,它会维护活动配置文件,这些配置文件很容易从系统 属性 spring.profiles.active 中检索。

当它通过组件注册相应的 bean 定义时,它会检查组件是否与某些 Condition 对象集匹配。其中之一是ProfileCondition,即

Condition that matches based on the value of a @Profile annotation.

例如,如果您有一个用 @Profile

注释的 @Bean 方法
@Bean
@Profile("foo-profile")
public Foo foo() {
    return new Foo();
}

Spring 将使用 ProfileConditionfoo-profile 与活动配置文件匹配。如果没有匹配,Spring 跳过这个 bean 定义。

它实际上从未存储所有可能的 @Profile 值。