replace an already created instance in spring with the one created at runtime -
i have spring framework related query.
could pls me in getting problem resolved. requirement replace created singleton instance 1 created @ runtime programmatically.
i have spring context defined in bean follows:
<bean name="configuration" class="com.myapp.tests.serviceconfiguration" /> <bean name="anotherbean class="com.myapp.tests.anotherbeanclass"> <property ref="configuration"/> </bean>
and loading the context using
applicationcontext ctx = classpathxmlapplicationcontext("appconfig.xml");
i need create new instance of com.myapp.tests.serviceconfiguration , replace "configuration" @ runtime , load other beans have dependency on this( kind of refresh.). in our case, anotherbean should see newly created serviceconfiguration instance after re-register singleton.
could pls kindly post solution new sort of spring requirements. error if try registersingleton says bean cant registered there. error infact correct, need sort of capability application.
thanks lot help.
autowirecapablebeanfactory factory = ctx.getautowirecapablebeanfactory(); beandefinitionregistry registry = (beandefinitionregistry) factory; genericbeandefinition beandefinition = new genericbeandefinition(); mutablepropertyvalues values = new mutablepropertyvalues(); values.addpropertyvalue("property1", "abc"); values.addpropertyvalue("property2", new runtimebeanreference("beanfromcontext")); beandefinition.setpropertyvalues(values); beandefinition.setbeanclass(serviceconfiguration.class); beandefinition.setautowirecandidate(true); registry.registerbeandefinition("configuration", beandefinition);
Comments
Post a Comment