什么是Starter?如何自定义一个Starter?如何使用?


前言

在Spring Boot中,Starter是一个特别的Maven或Gradle依赖包,旨在帮助开发者快速引入和配置某种功能或技术栈。Starter通常包含了一组相关的依赖和自动配置类,使开发者可以通过添加一个依赖项,轻松地在项目中启用复杂的功能。


一、Starter是什么?

1、Starter的作用

简化依赖管理

Spring Boot Starter将一组相关的库打包在一起,开发者不需要单独管理每个依赖项。例如,spring-boot-starter-web包含了所有构建Web应用所需的依赖。

自动配置

通过Spring Boot的自动配置机制,Starter可以在不需要大量手动配置的情况下启用功能。
Spring官方提供了很多Starter,用于各种常见的场景:

spring-boot-starter-web: 用于构建Web应用,包括Spring MVC和嵌入式Tomcat服务器。
spring-boot-starter-data-jpa: 用于与数据库交互,提供JPA和Hibernate的支持。
spring-boot-starter-security: 提供Spring Security的基础配置,方便实现安全认证和授权。
spring-boot-starter-thymeleaf: 集成Thymeleaf模板引擎,用于动态HTML页面生成。

2、自定义Starter

开发者也可以创建自己的Starter,比如你要实现一个邮件发送服务,或者封装一些公司内部的通用功能。通过自定义Starter,你可以:

封装功能:将公司或团队常用的功能封装到一个依赖中,简化其他项目的集成。
简化配置:通过自动配置类,让Starter在引入后能自动配置所需的Bean和其他配置项。

二、自定义邮件发送服务Starer

1.创建一个maven项目

命名为:email-spring-boot-starter
在这里插入图片描述

2、在pom.xml中定义项目的基础依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>email-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>email-spring-boot-starter</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>2.7.10</version>
        </dependency>

        <!-- 邮件依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.7.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
    </dependencies>
</project>

3. 创建邮件配置类

src/main/java/com/example/email 包中创建一个 EmailProperties 类,用于配置邮件服务:

package com.example.email;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "email")
public class EmailProperties {

    private String host;
    private int port;
    private String username;
    private String password;
}

4. 创建邮件发送服务类

在同一个包中创建一个 EmailService 类,包含发送邮件的核心逻辑:

package com.example.email;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    private final JavaMailSender mailSender;
    private final EmailProperties emailProperties;

    public EmailService(JavaMailSender mailSender, EmailProperties emailProperties) {
        this.mailSender = mailSender;
        this.emailProperties = emailProperties;
    }

    public void sendSimpleMessage(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailProperties.getUsername());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }
}

5、创建自动配置类

创建一个 EmailAutoConfiguration 类,使Spring Boot能够自动配置邮件服务:

package com.example.email;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;

@Configuration
@EnableConfigurationProperties(EmailProperties.class)
@ConditionalOnProperty(prefix = "email", name = "enabled", havingValue = "true")
public class EmailAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public EmailService emailService(JavaMailSender mailSender, EmailProperties emailProperties) {
        return new EmailService(mailSender, emailProperties);
    }
}

6、配置META-INF/spring.factories

为了让Spring Boot自动加载EmailAutoConfiguration,需要在 src/main/resources/META-INF/spring.factories 文件中进行配置:
新建一个spring.factories文件,注意目录层级
在这里插入图片描述
spring.factories文件中添加配置信息:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.email.EmailAutoConfiguration

7、打包并发布

使用 mvn clean install 命令将项目打包并发布到你自己的Maven仓库(如私有仓库或Nexus)中。
在这里插入图片描述
执行成功如下:
在这里插入图片描述

三、在自己的项目中使用

1、打开自己的另外一个项目,在pom.xml文件中引入依赖:

<dependency>
    <groupId>com.example</groupId>  <!-- 替换为你的实际groupId -->
    <artifactId>email-spring-boot-starter</artifactId>  <!-- Starter的artifactId -->
    <version>1.0.0</version>  <!-- 你为这个Starter定义的版本号 -->
</dependency>

2、在application.yml中配置邮件服务(此处以QQ邮箱为例)

密码的获取需要登录对应的邮箱在设置中获取,以下会给出详细教程

email:
  enabled: true
  host: smtp.qq.com  # 邮件服务器主机名
  port: 587  # 邮件服务器端口号
  username:   # 发送人qq邮箱地址(xxxxx@qq.com)
  password: # 邮件密码

获取邮箱password步骤

1、登录qq邮箱https://mail.qq.com/
2、点击右上角账号与安全
在这里插入图片描述
3、安全设置,如果这几个服务没开启,点击开启,然后点击生成授权码(复制粘贴到application.yml对应位置即可)
在这里插入图片描述

3、controller代码

package com.pro.server.controller;

import com.example.email.EmailService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/email")
public class EmailController {
    private final EmailService emailService;
    
    @PostMapping("/sendEmail")
    public String sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String text) {
        emailService.sendSimpleMessage(to, subject, text);
        return "邮件发送成功!";
    }
}

4、运行项目

确保一切配置完毕后,运行你的Spring Boot项目。项目启动后,你就可以通过调用API接口或在代码中直接使用EmailService类,来实现邮件发送功能。

四、通过ApiFox工具测试

测试接口示例:
在这里插入图片描述
测试结果:

在这里插入图片描述

总结

通过以上步骤,你在项目中成功集成了自定义的邮件发送服务Starter。你只需引入依赖并配置必要的属性,就可以方便地在项目中使用这个功能,大大简化了邮件发送服务的配置和使用过程。如有问题欢迎评论区交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值