首页 > 解决方案 > @ConfigurationProperties 在以编程方式添加活动配置文件后重新加载

问题描述

我有一个用@ConfigurationProperties 注释的类。

@ConfigurationProperties
@Configuration
@DependsOn("AddDefaultProfile")
public class MyProperties(){
 ....
}

我有几个配置文件chrome, safari, ie, and en_US, fr_CA等。我的运行参数看起来像-Dspring.profiles.active=chrome, en_US.

现在我想检查是否指定了语言,如果没有,我使用environment.addActiveProfile("en_US"). 问题是我的 MyProperties 没有重新加载。通过调试,我可以看到环境在活动配置文件中有 en_US,但 propertySource 仍然有旧的配置文件。

如何使用我的新配置文件重新加载 MyProperties。

标签: javaspringspring-bootjavabeansconfigurationproperties

解决方案


您可以尝试调用此方法WebApplicationContextUtils#initServletPropertySources(org.springframework.core.env.MutablePropertySources, javax.servlet.ServletContext, javax.servlet.ServletConfig),但我不确定这是否有用


推荐阅读