According to The IoC Container, Spring can manage a context hierarchy or hierarchy of containers and then use the <ref parent="beanId"/>
to refer to a bean in a parent context. What mechanism does Spring use to create this container hierarchy? Can one use the <import resource="application-context.xml"/>
command to create this hierarchy? Please provide an example of an application context that forms a 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 (?)