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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Kong Ingress Controller with Spring Boot Services
  • Authentication With Remote LDAP Server in Spring WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • Integrating Twilio Into My SaaS Solution In Heroku

Trending

  • Streamlining Event Data in Event-Driven Ansible
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  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.

By 
Trisha Gee user avatar
Trisha Gee
·
Nov. 26, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
15.9K 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 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

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

Related

  • Using Kong Ingress Controller with Spring Boot Services
  • Authentication With Remote LDAP Server in Spring WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • Integrating Twilio Into My SaaS Solution In Heroku

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!