Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring contexts hierarchy

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.

  1. Is it safe to have such hierarchy of contexts? For example if there are 30 child contexts?
  2. 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?
  3. 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.

like image 854
alsid Avatar asked Jan 14 '13 08:01

alsid


People also ask

What is context hierarchy?

@ContextHierarchy is a class-level annotation that is used to define a hierarchy of ApplicationContexts for integration tests.

What is context path in Spring?

Simply put, the context path is a name with which a web application is accessed. It is the root of the application. By default, Spring Boot serves the content on the root context path (“/”). So, any Boot application with default configuration can be accessed as: http://localhost:8080/

What is DispatcherServlet and ContextLoaderListener?

Basic. The task of the DispatcherServlet is to send request to the specific Spring MVC controller. ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web.xml ), parses it and loads the singleton bean defined in that config file.


1 Answers

  • 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.

like image 84
Abhinav Sarkar Avatar answered Sep 25 '22 17:09

Abhinav Sarkar