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

  • How to Create a Microservice Architecture With Java
  • The Most Popular Technologies for Java Microservices Right Now
  • How To Get Closer to Consistency in Microservice Architecture
  • Micro-Frontends in a Microservice Architecture

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Measuring the Impact of AI on Software Engineering Productivity
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Creating a Custom Starter With Spring Boot 3

Creating a Custom Starter With Spring Boot 3

A step-by-step tutorial on how to create a custom starter with Spring Boot 3.

By 
Konstantin Glumov user avatar
Konstantin Glumov
·
Sep. 05, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
7.8K Views

Join the DZone community and get the full member experience.

Join For Free

Starters are an integral part of the Spring Boot application. In addition to dependency versioning, they provide the ability to describe the configuration for a particular functionality. They gained their popularity due to the development of microservice architecture. When we have dozens or hundreds of services within one application, some of the functionality is not duplicated. For example, we need to connect the cache, database, logging etc. In order not to duplicate the same configuration every time, we simply add the starter as a dependency to our microservice, and Spring recognizes all our configurations.

We will try to create our own simple starter on Spring Boot 3. There have been some changes in it compared to the second version. Our starter will have the simplest functionality, namely to output a message to the console when our application starts. We create a project through spring initializer, which we will call according to the custom-spring-boot-starter convention

We connect it as a module in our application. In real projects, of course, it will be connected as an external dependency, but it is more convenient for us to do this in one project so that all the code lies in one place.

Spring boot project


Plug in the module in the settings. gradle file

gradle file

In order for our application to send a message to the console, you can create a Listener that, when updating the context, will output the line “Our spring boot starter is working!”. We also need to implement the onApplicationEvent method, where we will send the message.

Java
 
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class CustomListener implements ApplicationListener<ContextRefreshedEvent> {
    
  @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("Our spring boot starter is working!");
    }
}


Add Listener to the configuration

Java
 
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
public class CustomConfiguration {
  
    @Bean
    public CustomListener customListener() {
        return new CustomListener();
    }
}


In order for this configuration to be pulled into our main application, we need to add the META-INF/org. spring framework. boot. autoconfigure file.AutoConfiguration.imports, where the path for the configuration file will be specified. In the Spring 2 version, this file was called Spring. factories and was described in a slightly different way. Thus, we get an inversion of control, since we do not need to connect our starters in the code.

Spring boot file path


We add all our configurations to the file from a new line
Java code

We add our starter depending on the build file. Gradle


Gradle dependencies


We launched the application and it seems that the message was displayed at startup. Our starter has worked successfully.
Spring boot application started

How does it work? We have an annotation @SpringBootApplication that includes @EnableAutoConfiguration. When scanning packages, it finds all the imported files and connects the configurations that are described there.

If you want to migrate the starters from SB 2. x to SB 3, you should consider a few additional points.

  • You have to use Java 17+
  • Replace java import.* on Jakarta.*
  • For declared autoconfiguration, change @Configuration to @AutoConfiguration.
  • Trailing slash in controllers was removed by default
  • The mapping of properties has changed from spring. Redis to spring. data.redist
  • Spring-cloud-sleuth is absorbed by the micrometre project. Now the tracking of requests in the actuator is automatically enabled (that is, health checks, samples, etc. begin to be included in the trace).
  • Prometheus. The properties for enabling the metrics endpoint have changed.

Was management: metrics: export: prometheus: enabled: true 

Become management: prometheus: metrics: export: enabled: true


Architecture Database Dependency microservice Spring Boot Java (programming language)

Published at DZone with permission of Konstantin Glumov. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Create a Microservice Architecture With Java
  • The Most Popular Technologies for Java Microservices Right Now
  • How To Get Closer to Consistency in Microservice Architecture
  • Micro-Frontends in a Microservice Architecture

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!