DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Operator Overloading in Java
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • From On-Prem to SaaS
  • Auditing Tools for Kubernetes

Trending

  • Operator Overloading in Java
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • From On-Prem to SaaS
  • Auditing Tools for Kubernetes
  1. DZone
  2. Coding
  3. Frameworks
  4. Tutorial: Reactive Spring Boot, Part 5 – Auto-Configuration for Shared Beans

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

Learn more about auto-configuration for shared beans in this tutorial.

Trisha Gee user avatar by
Trisha Gee
·
Nov. 26, 19 · Tutorial
Like (7)
Save
Tweet
Share
15.10K Views

Join the DZone community and get the full member experience.

Join For Free

Coffee beans

Learn more about auto-configuration for shared beans in this tutorial.

Welcome 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 1: Building a Kotlin REST Service

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.

You may also like:  Java Annotated Monthly — November 2019

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 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.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 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.

Below is the full code is available on GitHub.

Further Reading

Tutorial: Reactive Spring Boot, Part 1: Building a Kotlin REST Service

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

Spring Framework Spring Boot intellij

Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Operator Overloading in Java
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • From On-Prem to SaaS
  • Auditing Tools for Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: