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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • A Walk-Through of the DZone Article Editor
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  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
8.6K 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

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook