How does Spring create an application context or container hierarchy? How does Spring create an application context or container hierarchy? spring spring

How does Spring create an application context or container hierarchy?


Application context hierarchy is created automatically; for example every Spring MVC application creates separate context for each DispatcherServlet. This context is a child of a common parent context. This way each child context can access beans from parent context, but not the other way around. Also sibling contexts are separated and invisible for each other.

You can create context hierarchy manually to provide finer level of granularity in your project. This can be achieved e.g. using various constructors of ClassPathXmlApplicationContext.

<import> construct merges beans from imported file, so it is a way of physically dividing bean definitions into several files, but they all end up in one context. BTW it's a pity there is no XML tag to define child context file (?)