我正在使用弹簧配置文件来管理不同的 bean 集。活动配置文件以编程方式设置为覆盖
org.springframework.context.ApplicationContextInitializer<ConfigurableApplicationContext>.initialize()
并使用
applicationContext.getEnvironment().setActiveProfiles(activeProfileEnv);
活动配置文件的来源是特定于环境的文件。因此,Environment 文件中的配置文件很可能与可用的配置文件(在 beans XML 上)不同,我想避免这种情况。我在 ConfigurableApplicationContext 上看不到“getAvailableProfiles”之类的访问器,我可以将其用于此目的。是否可以在设置之前验证活动配置文件值是否确实可用?
示例 XML
... <!-- Beans of all profiles -->
<beans profile="ProfileA">
...
</beans>
<beans profile="ProfileB">
...
</beans>
在这种情况下,我对可用配置文件的定义是“ProfileA”、“ProfileB”。在这种情况下,我不想将“ProfileC”设置为活动配置文件。