0

I want to change the value of my bean property in my application context without reading from properties file. I will get property values set in the properties object. properties object will be passed to my api while calling api interface.

Nep Tech
  • 1
  • 1

1 Answers1

1

You can do it through a custom ApplicationContextInitializer and using a PropertySource called PropertiesPropertySource

Create a custom ApplicationContextInitializer this way:

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 ));
    }

}

Register this ApplicationContextInitializer through web.xml file:

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>props.PropertyRegisterAppInitializer</param-value>
</context-param>
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125