Setting Spring bean property value in ApplicationContext without using properties file -
i want change value of bean property in application context without reading properties file. property values set in properties object. properties object passed api while calling api interface.
you can through custom applicationcontextinitializer
, using propertysource
called propertiespropertysource
create custom applicationcontextinitializer 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 applicationcontextinitializer
through web.xml
file:
<context-param> <param-name>contextinitializerclasses</param-name> <param-value>props.propertyregisterappinitializer</param-value> </context-param>
Comments
Post a Comment