How to Build a Custom Spring Boot Starter for SMS Services
This guide walks through creating a Spring Boot starter from scratch, covering Maven project setup, defining service and utility classes, configuring properties, implementing auto‑configuration with conditional annotations, registering the starter via spring.factories, and consuming it in another Spring Boot application.
Custom Spring Boot Starter Overview
Spring Boot’s core feature is auto‑configuration, which is enabled by a series of starter projects. Besides the official starters, developers can create their own by following a few rules.
Starter Preconditions
Provide a class that can be detected on the classpath.
When the condition is met, define a Bean and register it in the container.
Automatically configure any required settings for the project.
Step 1 – Create a Maven Project
Generate a simple Maven project (e.g., via IDE or mvn archetype:generate). The resulting structure is:
.
├── pom.xml
├── spring-boot-starter-msg.iml
└── src
├── main
└── testAdd the Spring Boot auto‑configuration dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>Step 2 – Define the Service Class
The MsgService class encapsulates the SMS‑sending logic and serves as a marker for the auto‑configuration condition.
package com.secbro2.msg;
import com.secbro2.utils.HttpClientUtils;
public class MsgService {
/** Access URL */
private String url;
/** Provider key ID */
private String accessKeyId;
/** Provider secret */
private String accessKeySecret;
public MsgService(String url, String accessKeyId, String accessKeySecret) {
this.url = url;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
}
public int sendMsg(String msg) {
// Call HTTP service and return result
return HttpClientUtils.sendMsg(url, accessKeyId, accessKeySecret, msg);
}
// getters/setters omitted
}The service relies on a simple utility class:
package com.secbro2.utils;
public class HttpClientUtils {
public static int sendMsg(String url, String accessKeyId, String accessKeySecret, String msg) {
// TODO: implement real HTTP request
System.out.println("Http请求,url=" + url + ";accessKeyId=" + accessKeyId + ";accessKeySecret=" + accessKeySecret + ";msg=" + msg);
return 0;
}
}Step 3 – Define Configuration Properties
The MsgProperties class binds the values from application.properties or application.yml using the msg prefix.
@ConfigurationProperties(prefix = "msg")
public class MsgProperties {
/** Access URL */
private String url;
/** Provider key ID */
private String accessKeyId;
/** Provider secret */
private String accessKeySecret;
// other fields, getters/setters omitted
}Step 4 – Implement the Auto‑Configuration Class
The auto‑configuration class wires everything together and is activated only when its conditions are satisfied.
@Configuration
@ConditionalOnClass(MsgService.class)
@EnableConfigurationProperties(MsgProperties.class)
public class MsgAutoConfiguration {
@Resource
private MsgProperties msgProperties;
@Bean
@ConditionalOnMissingBean(MsgService.class)
@ConditionalOnProperty(prefix = "msg", name = "enabled", havingValue = "true")
public MsgService msgService() {
MsgService msgService = new MsgService(
msgProperties.getUrl(),
msgProperties.getAccessKeyId(),
msgProperties.getAccessKeySecret()
);
// additional initialization if needed
return msgService;
}
}Key annotations: @Configuration – declares the class as a configuration source. @ConditionalOnClass – activates only if MsgService is on the classpath. @EnableConfigurationProperties – binds MsgProperties to external configuration. @Bean, @ConditionalOnMissingBean, @ConditionalOnProperty – control bean creation based on presence and property values.
Step 5 – Register via spring.factories
Create META-INF/spring.factories with the following entry:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secbro2.msg.MsgAutoConfigurationStep 6 – Use the Starter in Another Project
Add the starter as a Maven dependency:
<dependency>
<groupId>com.secbro2</groupId>
<artifactId>spring-boot-starter-msg</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>Configure the required properties:
msg.enabled=true
msg.url=127.0.0.1
msg.accessKeyId=10001
msg.accessKeySecret=afelwjfwfwefDefine a simple controller to test the service:
@RestController
public class HelloWorldController {
@Resource
private MsgService msgService;
@RequestMapping("/sendMsg")
public String sendMsg(){
msgService.sendMsg("测试消息");
return "";
}
}When accessing http://localhost:8080/sendMsg, the console prints:
Http请求,url=127.0.0.1;accessKeyId=10001;accessKeySecret=afelwjfwfwef;msg=测试消息This confirms that MsgService was auto‑configured and the starter works as intended.
Starter Workflow Summary
Spring Boot scans dependent JARs for spring.factories files.
It loads the listed AutoConfiguration classes.
Conditional annotations evaluate the environment and, if satisfied, register beans into the application context.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
