Tutorials

Tutorial: Reactive Spring Boot Part 5 – Auto-configuration for Shared Beans

In this lesson we look at how to use Spring beans from one module in a different module, using auto-configuration.

This is the fifth part of our tutorial showing how to build a Reactive application using Spring Boot, Kotlin, Java and JavaFX.  The original inspiration was a 70 minute live demo.

This blog post contains a video showing the process step-by-step and a textual walk-through (adapted from the transcript of the video) for those who prefer a written format.

This tutorial is a series of steps during which we will build a full Spring Boot application featuring a Kotlin back end, a Java client and a JavaFX user interface.

In the last lesson, we created a JavaFX Spring Boot application that shows an empty line chart.  In this video, we’re going to see how to set up auto-configuration for Spring Beans so that we can use beans defined in our stock-client module in the stock-ui module.

Adding a dependency upon another module

  1. Open the ChartController class we created in the last lesson.  This class is going to be responsible for updating the data we display on the line chart.
  2. Our ChartController needs to use the WebClientStockClient from the second lesson, it will use this to connect to the stock prices service. Create a new field for the client and make sure the class is imported.
public class ChartController {
    @FXML
    public LineChart<String, Double> chart;
    private WebClientStockClient webClientStockClient;
}
  1. We need to add a dependency upon stock-client from the stock-ui module
  2. (Tip: we can get get IntelliJ IDEA to add this dependency by pressing Alt+Enter on the red field, and selecting “Add maven dependency“. It will add the dependency in the Maven pom.xml file, so this dependency is managed by the build file not by the IDE.)
<dependency>
    <groupId>com.mechanitis</groupId>
    <artifactId>stock-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
  1. Add a constructor parameter so it gets wired in.
  2. (Tip: IntelliJ IDEA will offer to add a constructor parameter if we press Alt+Enter on the grey field name, or we can generate constructors).
public class ChartController {
    @FXML
    public LineChart<String, Double> chart;
    private WebClientStockClient webClientStockClient;

    public ChartController(WebClientStockClient webClientStockClient) {
        this.webClientStockClient = webClientStockClient;
    }
}

Creating the WebClientStockClient bean

IntelliJ IDEA helps us identify a problem in ChartController by telling us there are no beans of type WebClientStockClient available. Let’s fix this.

  1. In the stock-client module, we need to create a new Java class, ClientConfiguration, and add the Configuration annotation.  Here we’ll define our Beans.
  2. Create a method annotated with @Bean that returns a WebClientStockClient.  To create one of these we need to pass in a webClient parameter.
  3. (Tip: We can get IntelliJ IDEA to pass in a parameter for this by pressing Alt+Enter on the red webClient variable and selecting “create parameter”).
  4. Define another @Bean method, which returns a WebClient.  We can use the WebClient builder with default settings to create a new instance.
  5. We can also annotate this method with ConditionalOnMissingBean, which means this method will only be used to create the bean if a WebClient bean doesn’t already exist. We’re using this here because it’s possible that something else that uses this code might also create a WebClient.
@Configuration
public class ClientConfiguration {
    @Bean
    public WebClientStockClient webClientStockClient(WebClient webClient) {
        return new WebClientStockClient(webClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public WebClient webClient() {
        return WebClient.builder().build();
    }
}

Enabling Auto-configuration

Back in the stock-ui module, IntelliJ IDEA tells us we still can’t see this bean.  That’s because it was defined in a different module and this module can’t see the beans defined there. We’re going to use Spring Boot’s auto-configuration to help us here.

  1. Create a META-INF directory in stock-client src/main/resources.
  2. Inside this create a file, spring.factories.
  3. In spring.factories, set the property EnableAutoConfiguration to point to our ClientConfiguration class. This will give other modules that use this module access to the beans in our ClientConfiguration.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mechanitis.demo.stockclient.ClientConfiguration

Now when we go back to our ChartController class, it knows where to find the webClientStockClient bean.

Conclusion

This was a fairly small step in the tutorial, but it lets us create modules that can be re-used by different Spring Boot applications.  Now this step is complete, we can use the client in ChartController to connect to the price service and start showing real-time prices on the line chart.

Full code is available on GitHub. Discover more about IntelliJ IDEA’s features for Spring here.

image description