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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Java Microservices [Tutorials and Articles]
  • How to Create a Microservice Architecture With Java
  • How To Get Closer to Consistency in Microservice Architecture
  • Microservices Architecture on the Modern Stack of Java Technologies

Trending

  • How To Deploy Helidon Application to Kubernetes With Kubernetes Maven Plugin
  • Five Tools for Data Scientists to 10X their Productivity
  • Top 7 Best Practices DevSecOps Team Must Implement in the CI/CD Process
  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing
  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.

Konstantin Glumov user avatar by
Konstantin Glumov
·
Sep. 05, 23 · Tutorial
Like (3)
Save
Tweet
Share
3.78K 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

  • Java Microservices [Tutorials and Articles]
  • How to Create a Microservice Architecture With Java
  • How To Get Closer to Consistency in Microservice Architecture
  • Microservices Architecture on the Modern Stack of Java Technologies

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

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

Let's be friends: