Quick revision
The short answer
An ApplicationContext can have a parent: the child can see and inject every bean of its parent, but the parent cannot see the child's beans, and a child bean with the same name hides the parent's bean. Classic Spring MVC used this with a root context, loaded by ContextLoaderListener and holding services and repositories, and a child context per DispatcherServlet holding the controllers and web configuration. Spring Boot usually uses a single context instead.
What to remember
In this answer, we saw that in a parent-child ApplicationContext hierarchy the child can see and inject beans from its parent, the parent cannot see the child's beans, and a child bean with the same name hides the parent's. Classic Spring MVC used a root context loaded by ContextLoaderListener for services and repositories, and a child context per DispatcherServlet for controllers, which brought pitfalls such as duplicate scanning that lost transaction proxies. Spring Boot usually uses a single context, keeping hierarchies for cases such as a separate management port.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An ApplicationContext can have a parent: the child can see and inject every bean of its parent, but the parent cannot see the child's beans, and a child bean with the same name hides the parent's bean. Classic Spring MVC used this with a root context, loaded by ContextLoaderListener and holding services and repositories, and a child context per DispatcherServlet holding the controllers and web configuration. Spring Boot usually uses a single context instead.
Why this question is asked
Context hierarchies are less visible today, but they still appear in older applications, in Spring Boot's management server on a separate port, and in tests. They are also behind some classic puzzles, such as @Transactional working on a service in one place and not another, or a bean being created twice. Interviewers ask this to see whether we understand visibility between contexts and can recognise these situations. In this answer, we will recap what a context hierarchy is, show the visibility rules in code, walk through the classic Spring MVC setup and its pitfalls, and see where hierarchies still appear in Spring Boot.
1A Quick Recap of Context Hierarchies
An ApplicationContext is Spring's container. Normally an application has one. But a context can be created with a parent context, forming a tree. When a bean in the child needs a dependency, Spring looks in the child first and, if it finds nothing, in the parent, then the parent's parent, and so on. The search never goes downwards.
The idea behind it is sharing with separation. Several child contexts can share one parent's beans, for example common services, while each child keeps its own beans private from the others.
2The Visibility Rules in Code
A small program shows the three rules: the child sees the parent, the parent does not see the child, and a child bean with the same name hides the parent's bean.
- Child context (DispatcherServlet)controllers, view resolvers; sees itself and the parent
- Root context (ContextLoaderListener)services, repositories,
DataSource; sees only itself
2 layers, from the top down. Step through them one at a time.
Lookups go up the tree, never down.
Notice that the controller in the child was injected with OrderService from the parent without any special configuration. The hiding rule matters in practice: a bean defined again in the child silently wins for everything in the child, which can be useful for overrides and confusing when unintended.
3Where Classic Spring MVC Used It
Before Spring Boot, a Spring MVC web application was configured in web.xml or, later, with a WebApplicationInitializer. ContextLoaderListener, a servlet listener, created the root WebApplicationContext when the web application started, usually from applicationContext.xml or root configuration classes. That root context held the middle tier: services, repositories, the DataSource, the transaction manager and security.
Each DispatcherServlet, Spring MVC's front controller that receives requests and routes them to controllers, then created its own child WebApplicationContext, from a file named after the servlet such as dispatcher-servlet.xml or from servlet configuration classes. That child held the web tier: controllers, handler mappings, view resolvers and interceptors. An application could have two DispatcherServlets, say one for the website and one for an API, each with its own child context sharing the same root services.
6 messages in order. Play them, or step through them one at a time.
4Pitfalls of the Classic Setup
Post-processors work per context, so features enabled in one context do not apply to beans in the other. The classic bug: <tx:annotation-driven/> or @EnableTransactionManagement is declared in the root context, but component scanning in the servlet context also picks up the service package. The services are then created a second time in the child, without transaction proxies, and because the child's copy hides the parent's, controllers get the non-transactional copy. The fix was to scan only controllers in the child and everything else in the root, using include and exclude filters.
Property placeholders are also configured per context, so a placeholder configurer in the root does not resolve ${...} in the child's bean definitions, which is why such applications often declared one in each. Events propagate upwards only: an event published in the child also reaches listeners in the parent, but not the other way round.
5Spring Boot: Usually One Context
Spring Boot creates a single ApplicationContext that contains everything: services, repositories and controllers. The DispatcherServlet is registered as a bean in that context and uses it directly, so the split disappears, along with the duplicate-scanning pitfall.
Hierarchies still exist where they are useful. When Actuator runs on a separate management port, management.server.port, Spring Boot creates a child context for the management endpoints, with the main context as its parent. SpringApplicationBuilder can build explicit hierarchies with parent() and child(), for example to run two web applications sharing one set of services. Older Spring Cloud versions created a bootstrap parent context for configuration loading. And Spring's test support can build hierarchies with @ContextHierarchy. For ordinary services, though, one context is the norm.
6Follow-up questions
Can a bean in the parent receive a bean from the child?
No. Lookups only go up the tree, so a parent bean that depends on a child bean fails with a missing bean error. If the parent needs something defined by the web tier, the design should be inverted, for example by publishing an event or moving the bean into the parent.
Why would two child contexts be useful?
They let separate parts of an application, such as a public website and an internal API, have their own controllers, interceptors and view configuration without seeing each other, while sharing one set of services and one connection pool from the root context.
Do BeanPostProcessors in the parent apply to child beans?
No. Each context registers and applies its own post-processors. That is why transactions, AOP and placeholders must be enabled in the context whose beans need them, and it explains the classic bug where services scanned again in the servlet context lose their transactions.
7Conclusion
In this answer, we saw that in a parent-child ApplicationContext hierarchy the child can see and inject beans from its parent, the parent cannot see the child's beans, and a child bean with the same name hides the parent's. Classic Spring MVC used a root context loaded by ContextLoaderListener for services and repositories, and a child context per DispatcherServlet for controllers, which brought pitfalls such as duplicate scanning that lost transaction proxies. Spring Boot usually uses a single context, keeping hierarchies for cases such as a separate management port.
Open with
A context hierarchy is when an ApplicationContext is created with a parent context. Bean lookups start in the child and go up to the parent if nothing is found, so the child can inject any bean from its parent, but the parent cannot see anything in the child.
Then, if they want more
And if the child defines a bean with the same name as one in the parent, the child's bean hides the parent's for everything in the child. Classic Spring MVC applications used this. ContextLoaderListener created the root web application context at startup, from applicationContext.xml or root config classes, holding services, repositories, the DataSource and transactions.
Each DispatcherServlet then created its own child context, like dispatcher-servlet.xml, holding controllers, view resolvers and interceptors, and could share the root services; you could even have two servlets with separate child contexts.
The common pitfall was component scanning the service package again in the servlet context: the services were created twice, the child copies had no transaction proxies because transaction support was enabled only in the root, and since child beans hide parent beans, controllers got the non-transactional copy. Spring Boot normally uses just one context, so that problem goes away.
Hierarchies still show up when Actuator runs on a separate management port, which uses a child context, and when you build one explicitly with SpringApplicationBuilder parent and child.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a parent-child ApplicationContext hierarchy, and where did classic Spring MVC...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/9899cf9acf/