【发布时间】:2013-05-09 14:08:51
【问题描述】:
我想在我的应用程序上下文中更改我的 bean 属性的值,而不从属性文件中读取。我将获取属性对象中设置的属性值。在调用 api 接口时,properties 对象将传递给我的 api。
【问题讨论】:
我想在我的应用程序上下文中更改我的 bean 属性的值,而不从属性文件中读取。我将获取属性对象中设置的属性值。在调用 api 接口时,properties 对象将传递给我的 api。
【问题讨论】:
您可以通过自定义 ApplicationContextInitializer 和使用 PropertySource 来实现
叫PropertiesPropertySource
以这种方式创建自定义 ApplicationContextInitializer:
public class PropertyRegisterAppInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources();
Properties props = new Properties();
props.put("testkey", "testval");
sources.addFirst(new PropertiesPropertySource("propertiesSource", props ));
}
}
注册这个ApplicationContextInitializer到web.xml文件:
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>props.PropertyRegisterAppInitializer</param-value>
</context-param>
【讨论】: