我想在我的应用程序上下文中更改bean属性的值,而无需从属性文件中读取。我将获取属性对象中设置的属性值。在调用api接口时,属性对象将被传递给我的api。
答案 0 :(得分:1)
您可以通过自定义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>