WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Spring Framework Β· Expert Β· question 61 of 100

Explain how the ApplicationContext hierarchy works in Spring, and how it can be used for multi-module applications.?

πŸ“• Buy this interview preparation book: 100 Spring Framework questions & answers β€” PDF + EPUB for $5

The ApplicationContext hierarchy in Spring provides a way to structure your application using multiple contexts with a parent-child relationship. This can be useful in large-scale or multi-module applications where you may want to divide your application into smaller, more manageable sections that can be configured independently.

At the core of this hierarchy is the root application context, which serves as the parent context for all other contexts in the hierarchy. Each child context can access all the beans and resources of its parent context, but the parent context cannot access any beans or resources of its child contexts.

When it comes to multi-module applications, each module can have its own child application context that is a sub-context of the main application context. For example, if you have a web application with multiple modules like API, Auth, and Database, each can have its own configurable child context. You can define the configurations for each module, including its beans, properties, and other resources, in their respective child contexts.

When loading these child contexts in Spring, the hierarchy is set up so that the root application context is loaded first, followed by the child contexts. This means that the beans in the root context are available to all child contexts, but beans defined in a child context are not directly exposed to other child contexts.

To take advantage of ApplicationContext hierarchy in Spring, you can simply create a parent-child relationship and configure each context as you would normally. Here’s an example of how this can be done:

// Create a new root context
AbstractApplicationContext parentContext = new AnnotationConfigApplicationContext();
parentContext.refresh();

// Create the child context for the API module
AbstractApplicationContext childContext = new AnnotationConfigApplicationContext();
childContext.setParent(parentContext);
childContext.register(ApiConfig.class);
childContext.refresh();

// Create the child context for the Auth module
AbstractApplicationContext authContext = new AnnotationConfigApplicationContext();
authContext.setParent(parentContext);
authContext.register(AuthConfig.class);
authContext.refresh();

In this example, we create a root context using the AnnotationConfigApplicationContext class, which allows us to configure the context using Java-based configuration. We then create two child contexts, one for the API module and one for the Auth module, and set the parent context for each. Finally, we register the configuration classes for each child context and refresh the contexts.

By using ApplicationContext hierarchy in Spring, we can build modular, scalable, and more maintainable applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview β€” then scores it.
πŸ“ž Practice Spring Framework β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Spring Framework questions & answers β€” PDF + EPUB for $5

All 100 Spring Framework questions Β· All topics