问题
I'm going to create several Spring contexts with one parent context. Here is how I'm going to create parent context:
new ClassPathXmlApplicationContext(new String[] {"ApplicationContext/application.xml"})
And each parent context I want to create in following way:
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setProperties(properties);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(appContext);
context.addBeanFactoryPostProcessor(configurer);
context.setConfigLocation("ApplicationContext/beans.xml");
context.refresh();
The idea is to have multiple child contexts with the same bean hierarchy in each of them (DAOs, services, data source, transaction manager, etc). The reason to have several contexts is in demand to have several different data sources (one per each application context actually). Database structure for each data source is the same. So, there are some questions.
- Is it safe to have such hierarchy of contexts? For example if there are 30 child contexts?
- What about cross child context bean visibility? Say, I have CustomerService bean declared with @Component annotation with several autowired DAO dependencies. Does Spring perform autowiring and other DI actions within particular child context?
- Also, I'm going to lookup beans from child context using following method: childContext.getBean(CustomerService.class); Do I get the customer service from this specific child context and not other child context? I know, that spring singleton is a singleton per application context but still not sure.
PS. There is another way to handle multiple data sources described here. But this approach seems to be not really convenient in my case.
回答1:
- Is it safe to have such hierarchy of contexts? For example if there are 30 child contexts?
What do you mean by safety? If you mean thread safety while bean initialization then yes, since the contexts are initialized one by one.
- What about cross child context bean visibility? Say, I have CustomerService bean declared with @Component annotation with several autowired DAO dependencies. Does Spring perform autowiring and other DI actions within particular child context?
Beans are not visible across child contexts. The only beans visible in a context are its own and the ones in its parent contexts.
- Also, I'm going to lookup beans from child context using following method: childContext.getBean(CustomerService.class); Do I get the customer service from this specific child context and not other child context? I know, that spring singleton is a singleton per application context but still not sure.
Yes. As per the answer to the last question.
I use this pattern quite extensively in my applications. There is common context which is shared by many other child contexts by making it their parent. It is quite useful when you want to run completely isolated contexts inside a single JVM, for example if you application is a mutli-tenant one. Then you can start/stop/restart application contexts tenant-wise without restarting the JVM.
This also allows a clear separation of data sources and transaction managers and allows one to shard their databases easily.
来源:https://stackoverflow.com/questions/14315314/spring-contexts-hierarchy