Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

When might you deliberately use context hierarchies in a Spring Boot application today, for example with SpringApplicationBuilder.parent and child?

Today we use a context hierarchy only when we need several groups of beans that must be isolated from each other but share some common beans: for example several child contexts each running its own web server on a different port, plugin or tenant configurations that must not see each other's beans, or the parent context some Spring Cloud setups create. Hierarchies make events, post-processing and debugging harder, so we choose them only for a clear need.

Why this question is asked

Spring Boot hides context hierarchies so well that many developers never meet one, yet they still sit underneath features we use, such as the Actuator management port and per-client contexts in Spring Cloud. Interviewers ask this design question to see whether we know what a hierarchy buys, what it costs, and whether we can resist using a clever tool where a simpler one works. In this answer, we will recap what a parent and child context are, build one with SpringApplicationBuilder, walk through the situations where it is worth it, and then look at the costs that should make us hesitate.

1A Quick Recap of Parent and Child Contexts

An application context is the container that creates and wires our beans. A context can have a parent. When a bean in the child needs a dependency, Spring looks in the child first and then in the parent, so the child can use every parent bean. The parent cannot see the child's beans, and two children of the same parent, called siblings, cannot see each other's beans at all.

That gives us two properties at once: sharing, because the expensive or common beans live once in the parent, and isolation, because each child keeps its own beans, properties and even its own embedded web server.

2Building a Hierarchy With SpringApplicationBuilder

Spring Boot lets us declare a hierarchy fluently. Here a parent holds shared infrastructure, and two sibling children each start their own web server.

Java
@Configuration class SharedConfig { @Bean DataSource dataSource() { /* pooled */ } }
@SpringBootConfiguration @EnableAutoConfiguration class PublicApi { }
@SpringBootConfiguration @EnableAutoConfiguration class AdminApi { }
new SpringApplicationBuilder(SharedConfig.class).web(WebApplicationType.NONE)
.child(PublicApi.class).web(WebApplicationType.SERVLET)
.properties("server.port=8080")
.sibling(AdminApi.class).web(WebApplicationType.SERVLET)
.properties("server.port=9090")
.run(args);
// Tomcat started on port 8080 (PublicApi context)
// Tomcat started on port 9090 (AdminApi context)
// both use the one DataSource from SharedConfig
Step throughA parent with two sibling children
  1. PublicApi context, port 8080public controllers, its own MVC setup and filters
  2. AdminApi context, port 9090admin controllers, stricter security
  3. Shared parent contextDataSource, domain services, caches used by both

3 layers, from the top down. Step through them one at a time.

Siblings share the parent's beans but cannot see each other's beans.

Notice what each piece gives us. The DataSource and its connection pool exist once, so we do not double the number of database connections. The admin controllers cannot be reached on port 8080 because they are not even beans in that context, which is a stronger guarantee than a path rule in a security filter.

3Situations Where a Hierarchy Earns Its Place

The first case is the one above: several servers or entry points on different ports that must stay separate but share common beans. Spring Boot itself uses this pattern when we set management.server.port to a different port: Actuator's endpoints run in a child context with their own server, so they can be firewalled separately.

The second case is Spring Cloud. Older Spring Cloud versions created a bootstrap context as the parent of the application context, to load configuration from a config server before anything else. Since Spring Cloud 2020.0 the recommended approach is spring.config.import and the bootstrap context is opt-in, but we still meet it in older services. Spring Cloud also creates small child contexts per named client, for example per load-balanced service, so each client can have its own configuration beans without clashing (check your Spring Cloud version for which projects still do this).

The third case is isolation of plugins or tenants whose configurations must not see each other's beans. If tenant A defines a PricingRule bean and tenant B defines a different one, putting each in its own child context means type-based injection in one tenant can never pick up the other's bean, while both share the platform services in the parent.

4What a Hierarchy Costs

A hierarchy is not free, and most of the cost lands on whoever debugs it later. Events propagate in one direction only: an event published in a child is also delivered to the parent's listeners, but an event published in the parent never reaches listeners in the children. Post-processing does not cross the boundary either, so @EnableTransactionManagement, @EnableAsync or an aspect in the parent does nothing for child beans unless the child registers its own.

Lookups also become surprising. A child can define a bean with the same name or type as one in the parent and quietly shadow it, so the same code gets a different bean depending on where it runs. Auto-configuration conditions such as @ConditionalOnMissingBean may or may not search the parent depending on their search setting, which changes what gets created. Shutdown order, health checks and the Actuator beans endpoint all become per context, and a stack trace no longer tells us which context a bean came from.

What crosses the parent-child boundary
MechanismChild -> parentParent -> child
Bean lookup and injectionyes, child sees parent beansno
Application eventsyes, child events reach parent listenersno
BeanPostProcessors and aspectsno, each context has its ownno
Environment propertiesparent property sources are merged into the childno
Most surprises come from the rows that say no.

5How to Decide

Before reaching for a hierarchy, I would try the simpler tools. Profiles and @ConditionalOnProperty switch configuration on and off within one context. Qualifiers separate two beans of the same type. A second port for a few endpoints may be better served by a reverse proxy. Separate deployable services give far stronger isolation than child contexts, at the cost of more processes.

I would choose a hierarchy when there is a real need for both sharing and in-process isolation: shared expensive resources such as a connection pool or a large in-memory model, plus groups of beans that must provably not see each other. When I do, I keep the parent small and stable, document which beans live where, and register the needed infrastructure, such as transaction management, explicitly in each child.

6Follow-up questions

Does a child context get the parent's @ConfigurationProperties values?

The child's Environment includes the parent's property sources, so properties defined for the parent are visible when the child binds its own @ConfigurationProperties. Properties set only on the child, like server.port in the builder example, are not visible to the parent. The parent's already bound properties beans can also be injected into the child as ordinary beans.

How do we close a hierarchy correctly?

Closing a child closes only that child. Closing the parent does not automatically close children that were created separately, unless they were created through the builder, which registers them so the whole tree shuts down in order. We should close children before the parent so that child beans can still use parent resources, such as the DataSource, during their own shutdown.

Is a context hierarchy a good way to build multi-tenancy?

Only for a small, fixed number of tenants with very different configurations. Each child context costs startup time and memory, so a few hundred tenants would make startup slow and heavy. For many tenants with similar configuration, we usually keep one context and pass the tenant as data, for example by routing to a per-tenant DataSource.

7Conclusion

In this answer, we saw that a context hierarchy gives a child access to its parent's beans while keeping siblings isolated from each other. That is worth it for several servers on different ports that share common beans, for the parent and per-client contexts in some Spring Cloud setups, and for plugin or tenant configurations that must not see each other's beans. The price is one-way events, post-processing that does not cross contexts, shadowed beans and harder debugging, so we use a hierarchy only when both the sharing and the isolation are real requirements.

What to say in the interviewAbout 80 seconds aloud

Open with

I would use a context hierarchy only when I need both sharing and isolation inside one process. The textbook case is several web servers on different ports, for example a public API on 8080 and an admin API on 9090, built with SpringApplicationBuilder: the parent holds the DataSource and domain services once, and each child has its own server and controllers, so admin endpoints are not even beans on the public port.

Then, if they want more

Spring Boot does the same for Actuator when the management port differs. Some Spring Cloud setups use a bootstrap parent context, although since 2020.0 spring.config.import is preferred, and Spring Cloud creates small child contexts per named client. The third case is plugins or tenants whose bean definitions must not see each other.

But I am careful, because hierarchies have real costs. Events from a child reach the parent but not the other way round. BeanPostProcessors do not cross contexts, so transactions or aspects enabled in the parent do not apply to child beans. A child can shadow a parent bean silently, and debugging which context produced a bean is harder.

So I first try profiles, qualifiers, conditional configuration or separate services, and choose a hierarchy only when I can name the shared resource and the isolation requirement.

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.
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 "When might you deliberately use context hierarchies in a Spring Boot application today...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/8ffb029cc9/