Tutorial: Reactive Spring Boot, Part 5 – Auto-Configuration for Shared Beans
Learn more about auto-configuration for shared beans in this tutorial.
Join the DZone community and get the full member experience.
Join For FreeWelcome to the fifth installment of this series on how to build a Reactive web application using Spring Boot, Kotlin, Java, and JavaFX. The original inspiration was a 70-minute live demo.
Here is everything you've missed so far:
Tutorial: Reactive Spring Boot, Part 2: A REST Client for Reactive Streams
Tutorial: Reactive Spring Boot, Part 3: A JavaFX Spring Boot Application
Tutorial: Reactive Spring Boot, Part 4: A JavaFX Line Chart
In this lesson, we look at how to use Spring beans from one module in a different module using auto-configuration.
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
- 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. - Our
ChartController
needs to use theWebClientStockClient
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;
}
- We need to add a dependency upon stock-client from the stock-ui module
- (Tip: We can 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>
- Add a constructor parameter so it gets wired in.
- (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.
- 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. - Create a method annotated with @Bean that returns a
WebClientStockClient
. To create one of these, we need to pass in aWebClient
parameter. - (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”). - Define another @Bean method, which returns a
WebClient
. We can use theWebClient
builder with default settings to create a new instance. - 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 aWebClient
.
@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.
- Create a META-INF directory in stock-client src/main/resources.
- Inside this create a file, spring.factories.
- In spring.properties, set the property EnableAutoConfiguration to point to our
ClientConfiguration
class. This will give other modules that use this module access to the beans in ourClientConfiguration
.
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.
Below is the full code is available on GitHub.
Further Reading
Tutorial: Reactive Spring Boot, Part 2: A REST Client for Reactive Streams
Tutorial: Reactive Spring Boot, Part 3: A JavaFX Spring Boot Application
Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments