Skip to content

Multiple Spring ApplicationContext Pattern

The Multiple Spring ApplicationContext Pattern involves structuring a Spring application using a hierarchy of AnnotationConfigApplicationContext instances.^[600-developer__java__spring__multipleApplicationContext.md] This approach establishes a parent-child relationship between contexts, allowing for hierarchical bean management and environment configuration.

Context Hierarchy

In this pattern, multiple context objects are instantiated and explicitly linked using the setParent(ApplicationContext parent) method^[600-developer__java__spring__multipleApplicationContext.md]. This creates a tree structure where a child context is associated with a parent context.

Environment Merging

When a parent context is set, the child context inherits and merges its environment configuration.^[600-developer__java__spring__multipleApplicationContext.md] If the parent context is non-null and its environment is an instance of ConfigurableEnvironment, the child's environment will merge the parent's environment settings via ConfigurableEnvironment.merge().^[600-developer__java__spring__multipleApplicationContext.md]

Bean Visibility

A key characteristic of the context hierarchy is the unidirectional visibility of beans^[600-developer__java__spring__multipleApplicationContext.md].

  • Child to Parent: Child contexts can access and retrieve beans defined in their parent (or root) contexts^[600-developer__java__spring__multipleApplicationContext.md].
  • Parent to Child: Conversely, parent contexts cannot access beans defined within their child contexts^[600-developer__java__spring__multipleApplicationContext.md].

For example, if childCtx2 is a descendant of parentCtx, childCtx2 can successfully retrieve a bean (e.g., Car) from parentCtx, but parentCtx cannot retrieve beans (e.g., Dog, Egg) from the child contexts^[600-developer__java__spring__multipleApplicationContext.md].

Lifecycle Management

Each context in the hierarchy must be individually managed, including registration and refreshment^[600-developer__java__spring__multipleApplicationContext.md].

  • Initialization: Beans are registered to specific contexts (e.g., ctx.registerBean(Config.class)), and each context must be explicitly refreshed (e.g., ctx.refresh()) to initialize its beans^[600-developer__java__spring__multipleApplicationContext.md].
  • Destruction: Closing a context using the close() method triggers the destruction of that specific context's resources^[600-developer__java__spring__multipleApplicationContext.md]. It is possible to verify the active status of a context using the isActive() method^[600-developer__java__spring__multipleApplicationContext.md].

Closing a parent context eventually affects the availability of the application context structure, but child contexts can be instantiated and refreshed at different times, even after a parent has been initialized^[600-developer__java__spring__multipleApplicationContext.md].

Sources