2013-05-09 57 views

回答

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> 
相關問題