Context Hierarchy with the Spring Boot Fluent Builder API – 使用Spring Boot Fluent Builder API的语境层次结构

最后修改: 2018年 5月 30日

1. Overview


It’s possible to create separate contexts and organize them in a hierarchy in Spring Boot.

在Spring Boot中,可以创建独立的上下文,并将它们组织成一个层次结构。

A context hierarchy can be defined in different ways in Spring Boot application. In this article, we’ll look at how we can create multiple contexts using the fluent builder API.

在Spring Boot应用程序中,可以通过不同的方式来定义上下文层次结构。在这篇文章中,我们将探讨如何使用流畅的构建器API创建多个上下文

As we won’t go into details on how to set up a Spring Boot application, you might want to check out this article.

由于我们不会详细介绍如何设置Spring Boot应用程序,您可能想看看这篇文章

2. Application Context Hierarchy


We can have multiple application contexts that share a parent-child relationship.


A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.


Furthermore, we can use contexts to prevent beans registered in one context from being accessible in another. This facilitates the creation of loosely coupled modules.


Here some points worth noting are that a context can have only one parent while a parent context can have multiple child contexts. Also, a child context can access beans in the parent context but not vice-versa.


3. Using SpringApplicationBuilder API

3.使用SpringApplicationBuilder API

The SpringApplicationBuilder class provides a fluent API to create a parent-child relationship between contexts using parent(), child() and sibling() methods.


To exemplify the context hierarchy, we’ll set up a non-web parent application context with 2 child web contexts.


To demonstrate this, we’ll start two instances of embedded Tomcat each with its own web application context and both running in a single JVM.


3.1. Parent Context


To begin, let’s create a service bean along with a bean definition class which reside in the parent package. We want this bean to return a greeting which is displayed to the client of our web application:


public class HomeService implements IHomeService {

    public String getGreeting() {
        return "Welcome User";

And the bean definition class:


public class ServiceConfig {}

Next, we’ll create the configuration for the two child contexts.


3.2. Child Context


Since all contexts are configured using the default configuration file, we need to provide separate configurations for properties which cannot be shared among contexts such as server ports.


To prevent conflicting configurations being picked up by the auto-configuration, we’ll also keep the classes in separate packages.


Let’s start by defining a properties file for the first child context:




Note that we’ve configured the port and context path, as well as a JMX name so the application names don’t conflict.


Let’s now add the main configuration class for this context:


public class Ctx1Config {
    public IHomeService homeService() {
        return new GreetingService();

This class provides a new definition for the homeService bean that will overwrite the one from the parent.

这个类为homeService bean提供了一个新的定义,它将覆盖来自父类的定义。

Let’s see the definition of the GreetingService class:


public class GreetingService implements IHomeService {

    public String getGreeting() {
        return "Greetings for the day";

Finally, we’ll add a controller for this web context that use the homeService bean to display a message to the user:

最后,我们将为这个Web上下文添加一个控制器,使用homeService Bean来向用户显示一个消息。

public class Ctx1Controller {

    private HomeService homeService;

    public String greeting() {
        return homeService.getGreeting();

3.3. Sibling Context


For our second context, we’ll create a controller and configuration class which are very similar to the ones in the previous section.


This time, we won’t create a homeService bean – as we’ll access it from the parent context.

这一次,我们不会创建一个homeService bean – 因为我们将从父级上下文中访问它。

First, let’s add a properties file for this context:




And the configuration class for the sibling application:


public class Ctx2Config {}

Let’s also add a controller, which has HomeService as a dependency:


public class Ctx2Controller {

    private IHomeService homeService;

    public String getGreeting() {
        return homeService.getGreeting();

In this case, our controller should get the homeService bean from the parent context.

在这种情况下,我们的控制器应该从父级上下文中获得homeService Bean。

3.4. Context Hierarchy


Now we can put everything together and define the context hierarchy using SpringApplicationBuilder:


public class App {
    public static void main(String[] args) {
        new SpringApplicationBuilder()

Finally, on running the Spring Boot App we can access both applications at their respective ports using localhost:8074/ctx1/home and localhost:8075/ctx2/greeting.

最后,在运行Spring Boot应用程序时,我们可以使用localhost:8074/ctx1/homelocalhost:8075/ctx2/greeting.在各自的端口访问两个应用程序。

4. Conclusion


Using the SpringApplicationBuilder API, we first created a parent-child relationship between two contexts of an application. Next, we covered how to override the parent configuration in the child context. Lastly, we added a sibling context to demonstrate how the configuration in the parent context can be shared with other child contexts.

使用SpringApplicationBuilder API,我们首先在一个应用程序的两个上下文之间创建了一个父子关系。接下来,我们介绍了如何在子上下文中覆盖父级配置。最后,我们添加了一个兄弟姐妹上下文,以演示如何将父上下文中的配置与其他子上下文共享。

The source code of the example is available over on GitHub.