Backend Development 12 min read

Build Your Own Spring Boot Starter for SMS Integration – Step‑by‑Step Guide

This article walks you through creating a custom Spring Boot starter for SMS services, covering naming conventions, Maven dependencies, configuration properties, auto‑configuration, sender implementations, activation methods, packaging, and usage with practical code examples and diagrams.

macrozheng
macrozheng
macrozheng
Build Your Own Spring Boot Starter for SMS Integration – Step‑by‑Step Guide

Introduction

After discussing the concept of Spring Boot starters, we dive straight into building our own starter for sending SMS messages.

Starters are a set of convenient dependency descriptors that you can include in your application. They provide a one‑stop shop for all the Spring and related technologies you need without hunting through sample code.

What Is a Spring Boot Starter?

A starter bundles the necessary dependencies and auto‑configuration for a specific feature, allowing developers to add functionality with a single dependency and minimal configuration.

Why Starters Matter

Before starters, integrating components required manually adding Maven dependencies, writing configuration files, and troubleshooting jar conflicts. Starters simplify this process, embodying the "convention over configuration" principle.

Naming Conventions

Official starters follow

spring-boot-starter-*

. Third‑party starters should avoid the

spring-boot

prefix and typically use

{project}-spring-boot-starter

, e.g.,

mybatis-spring-boot-starter

.

Creating a Custom SMS Starter

1. Add Maven Dependencies

<code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
    &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;version&gt;1.16.18&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;</code>

2. Define Configuration Properties

<code>@ConfigurationProperties(prefix = "sms")
@Data
public class SmsProperties {
    private SmsMessage aliyun = new SmsMessage();
    private SmsMessage tencent = new SmsMessage();

    @Data
    public static class SmsMessage {
        private String userName;
        private String passWord;
        private String sign;
        private String url;

        @Override
        public String toString() {
            return "SmsMessage{" +
                    "userName='" + userName + '\'' +
                    ", passWord='" + passWord + '\'' +
                    ", sign='" + sign + '\'' +
                    ", url='" + url + '\'' +
                    '}';
        }
    }
}
</code>

Example

application.yml

:

<code>sms:
  aliyun:
    pass-word: 12345
    user-name: java金融
    sign: 阿里云
    url: http://aliyun.com/send
  tencent:
    pass-word: 6666
    user-name: java金融
    sign: 腾讯云
    url: http://tencent.com/send
</code>

3. Auto‑Configuration Class

<code>@EnableConfigurationProperties(SmsProperties.class)
@Configuration
public class SmsAutoConfiguration {
    @Bean
    public AliyunSmsSenderImpl aliYunSmsSender(SmsProperties smsProperties) {
        return new AliyunSmsSenderImpl(smsProperties.getAliyun());
    }

    @Bean
    public TencentSmsSenderImpl tencentSmsSender(SmsProperties smsProperties) {
        return new TencentSmsSenderImpl(smsProperties.getTencent());
    }
}
</code>

4. Sender Implementations

<code>public class AliyunSmsSenderImpl implements SmsSender {
    private SmsMessage smsMessage;

    public AliyunSmsSenderImpl(SmsMessage smsMessage) {
        this.smsMessage = smsMessage;
    }

    @Override
    public boolean send(String message) {
        System.out.println(smsMessage.toString() + "开始发送短信==》短信内容:" + message);
        return true;
    }
}

public class TencentSmsSenderImpl implements SmsSender {
    private SmsMessage smsMessage;

    public TencentSmsSenderImpl(SmsMessage smsMessage) {
        this.smsMessage = smsMessage;
    }

    @Override
    public boolean send(String message) {
        System.out.println(smsMessage.toString() + "开始发送短信==》短信内容:" + message);
        return true;
    }
}
</code>

5. Enable Annotation

<code>@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({SmsAutoConfiguration.class})
public @interface EnableSms {}
</code>

6. Using the Starter

<code>@SpringBootApplication
@EnableSms
public class AutoconfigApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(AutoconfigApplication.class, args);
        AliyunSmsSenderImpl aliyun = ctx.getBean(AliyunSmsSenderImpl.class);
        aliyun.send("用阿里云发送短信");
        TencentSmsSenderImpl tencent = ctx.getBean(TencentSmsSenderImpl.class);
        tencent.send("用腾讯云发送短信");
    }
}
</code>

Running the application prints the configured message details for both Alibaba Cloud and Tencent Cloud SMS services.

Packaging

Execute

mvn install

for a local build or deploy the artifact to a corporate repository.

Conclusion

Spring Boot starters dramatically simplify component integration by encapsulating dependencies and configuration, allowing developers to focus on business logic while providing a clean, extensible way for others to use the functionality.

backendjavaSpring BootAuto‑ConfigurationSMSStarter
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.