I've read that dynamic bean definition change. I try it in a simple code example (see code below), and I find it very attractive in situations where I don't want to stop server but add/change bean definition.
Questions:
- Is it safe do to so (see code below)?
I've read that it is possible to achieve bean definition change in runtime with help of
StaticApplicationContex
orBeanPostProcessor
orBeanFactoryPostProcessor
? So what is the difference?public class Main { final static String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xmlns:context=\"http://www.springframework.org/schema/context\"\n" + " xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd\">\n" + " <context:annotation-config />\n" + " <context:component-scan base-package=\"vbah\"/>"; final static String contextA = "<bean id=\"test\" class=\"java.lang.String\">\n" + "\t\t<constructor-arg value=\"fromContextA\"/>\n" + "</bean></beans>"; final static String contextB = "<bean id=\"test\" class=\"java.lang.String\">\n" + "\t\t<constructor-arg value=\"fromContextB\"/>\n" + "</bean></beans>"; public static void main(String[] args) throws IOException { //create a single context file final File contextFile = new File("src/resources/spring-config.xml"); //write the first context into it FileUtils.writeStringToFile(contextFile, header + contextA); //create a spring context FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( new String[]{contextFile.getPath()} ); //echo "fromContextA" System.out.println(context.getBean("test")); //write the second context into it FileUtils.writeStringToFile(contextFile, header + contextB); //refresh the context context.refresh(); //echo "fromContextB" System.out.println(context.getBean("test")); } }
EDIT:
Can you answer the questions below:
- As I understand
BeanPostProcess
allow you to modify already existed bean instances at runtime by wrapping the object with proxy. Am I right? AbstractApplicationContext#refresh() drop all singleton beans and recreate them.
- But If I want to change the definition of prototype/custom scoped bean?
- If I've got two beans: A and B. A has reference to B. If I change the bean definition in such way that it doesn't contain definition of B. Than B instances will be destroyed, but new instances won't be created. Than A will get a
null
dependency. Am I right?
StaticApplicationContext
andBeanFactoryPostProcessor
both allow me to change a bean definition in runtime. But what are the difference, pros/cons?- [Main question] Why Spring has 3 mechanism to achieve the same goal. Can you make a brief compoarison (or usecases examples) between
AbstractApplicationContext#refresh()
,StaticApplicationContext
andBeanFactoryPostProcessor
please.