Spring context hierarchy

I am going to create multiple Spring contexts with one parent context. This is how I am going to create the parent context:

new ClassPathXmlApplicationContext(new String[] {"ApplicationContext/application.xml"}) 

And each parent context that I want to create as follows:

 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 several child contexts with the same bean hierarchy in each of them (DAO, services, data source, transaction manager, etc.). The reason for having multiple contexts is the demand for several different data sources (one for each application context). The database structure for each data source is the same. So there are some questions.

  • Is it safe to have such a hierarchy of contexts? For example, if there are 30 child contexts?
  • What about cross-child bean context transparency? Say I have a CustomerService bean with @Component annotation with several DAO dependent dependencies. Does Spring do auto-automation and other DI actions in a specific child context?
  • Also, I'm going to look for beans from a child context using the following method: childContext.getBean (CustomerService.class); Am I getting customer service from this particular child context, and not another child context? I know that Spring singleton is a one-point application context, but still not sure.

PS. There is another way to handle the multiple data sources described here . But this approach is not very convenient in my case.

+7
source share
1 answer
  • Is it safe to have such a hierarchy of contexts? For example, if there are 30 child contexts?

What do you mean by security? If you mean thread safety when initializing a bean, then yes, because contexts are initialized one by one.

  • What about cross-child bean context transparency? Say I have a CustomerService bean with @Component annotation with several DAO dependent dependencies. Does Spring do auto-automation and other DI actions in a specific child context?

Beans are not displayed in child contexts. The only beans visible in the context are its own and those contained in its parent contexts.

  • Also, I'm going to look for beans from a child context using the following method: childContext.getBean (CustomerService.class); Am I getting customer service from this particular child context, and not another child context? I know that Spring singleton is a one-point application context, but still not sure.

Yes. In accordance with the answer to the last question.

I use this pattern quite widely in my applications. There is a common context that is shared by many other child contexts, making it its parent. This is very useful when you want to run completely isolated contexts within the same JVM, for example if you use mutli-tenant. Then you can start / stop / restart application contexts without using restarting the JVM.

It also provides a clear separation of data sources and transaction managers and makes it easy to outline their databases.

+11
source

All Articles