Springboot自定义starter

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

1、问题描述

  在做springboot项目中,我们会发现经常依赖spring-boot-starter-xxx的pom,知道这种starter为了解决某种业务场景的问题内部其实做了很多maven依赖的封装,使得引用者不需要配置繁杂的pom,带着好奇心研究了下这个技术,网上找到一些资料,这里摘抄一段:SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。(引用https://www.cnblogs.com/hello-shf/p/10864977.html )

        命名规则:SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。SpringBoot提供的常见的starter有:(参考:https://www.cnblogs.com/selfchange/p/10014312.html)

  • spring-boot-starter
  • spring-boot-starter-activemq
  • spring-boot-starter-amqp
  • spring-boot-starter-aop
  • spring-boot-starter-cache
  • spring-boot-starter-data-elasticsearch
  • spring-boot-starter-data-jdbc
  • spring-boot-starter-data-mongodb
  • spring-boot-starter-data-redis

2、操作方法

1、demo思路

通过封装一个邮件发送的starter,可以支持国外常见的AWS SES, SendGrid两种方式,通过引用email-springboot-starter后能够自动装载配置的方式实例化对象供外部直接调用。

2、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>email-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>email-springboot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 自定义配置包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--添加了该模块以来的模块,再配置yml时候会有本模块配置属性的提示信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--阿里json操作-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
    </dependencies>

</project>

3、配置实体类

@ConfigurationProperties(prefix = "email")这个会自动对应到email开头的配置项目

package email.starter.properties;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
/** 
 * @Description: 配置信息实体 
 */
@ConfigurationProperties(prefix = "email")
public class EmailProperties {

    private MailConfig awsSesConfig = new MailConfig();

    private MailConfig sendGridConfig = new MailConfig();

    public MailConfig getAwsSesConfig() {
        return awsSesConfig;
    }

    public MailConfig getSendGridConfig() {
        return sendGridConfig;
    }

    @Getter
    @Setter
    @ToString
    public class MailConfig{

        private String userName;

        private String password;

        private String sender;

    }

}

4、定义一个接口

package email.starter.service;
 
/**
 *  Email接口
 */
public interface EmailService {
 
    boolean sendMail(String message);
 
}

5、AWS SES实现类

package email.starter.service;

import email.starter.properties.EmailProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * AWS SES邮件发送实现
 */
public class AwsSesEmailServiceImpl implements EmailService{

    private EmailProperties.MailConfig mailConfig;

    public AwsSesEmailServiceImpl(EmailProperties.MailConfig mailConfig) {
        this.mailConfig = mailConfig;
    }

    @Override
    public boolean sendMail(String message) {
        System.out.println(mailConfig.toString()+" SendEmail->message:"+message);
        return true;
    }

}

6、SendGrid实现类

package email.starter.service;

import email.starter.properties.EmailProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * SendGrid邮件发送实现
 */
public class SendGridEmailServiceImpl implements EmailService{

    private EmailProperties.MailConfig mailConfig;

    public SendGridEmailServiceImpl(EmailProperties.MailConfig mailConfig) {
        this.mailConfig = mailConfig;
    }

    @Override
    public boolean sendMail(String message) {
        System.out.println(mailConfig.toString()+" SendEmail->message:"+message);
        return true;
    }

}

7、装载配置并实例化服务类

@Configuration :声明这是一个配置对象
@EnableConfigurationProperties(EmailProperties.class) :使EmailProperties对象生效,将其注入IOC容器之中。

package email.starter.config;
 
import email.starter.properties.EmailProperties;
import email.starter.service.AwsSesEmailServiceImpl;
import email.starter.service.SendGridEmailServiceImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@EnableConfigurationProperties(EmailProperties.class)
public class EmailAutoConfiguration {
 
    @Bean
    public AwsSesEmailServiceImpl getAwsSesEmailServiceImpl(EmailProperties emailProperties){
        return new AwsSesEmailServiceImpl(emailProperties.getAwsSesConfig());
    }

    @Bean
    public SendGridEmailServiceImpl getSendGridEmailServiceImpl(EmailProperties emailProperties){
        return new SendGridEmailServiceImpl(emailProperties.getSendGridConfig());
    }
 
}

8、maven install 

9、调用方引用

         <dependency>
            <groupId>com.demo</groupId>
            <artifactId>email-springboot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

10、配置文件

email:
  aws-ses-config:
    sender: 'mao2080@sina.com'
    password: 'admin'
    user-name: 'AwsSes'
  send-grid-config:
    sender: 'mao2080@sina.com'
    password: 'admin'
    user-name:  'SendGrid'

11、调用代码

package email;

import email.starter.service.AwsSesEmailServiceImpl;
import email.starter.service.EmailService;
import email.starter.service.SendGridEmailServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        EmailService awsSesEmailService = applicationContext.getBean(AwsSesEmailServiceImpl.class);
        awsSesEmailService.sendMail("Hello World !");
        EmailService sendGridEmailService = applicationContext.getBean(SendGridEmailServiceImpl.class);
        sendGridEmailService.sendMail("Hello World !");
    }

}

12、执行效果

这样就很简单的可以使用email-springboot-starter调用邮件发送接口了。

"C:\Program Files (x86)\Java\jdk1.8.0_192\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=51328 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:F:\work\ideaIU-2018.1.6.win\lib\idea_rt.jar=51329:F:\work\ideaIU-2018.1.6.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\rt.jar;F:\work\workspace-demo\email-springboot-test\target\classes;F:\work\repository\org\springframework\boot\spring-boot-starter-web\2.4.1\spring-boot-starter-web-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot-starter\2.4.1\spring-boot-starter-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot\2.4.1\spring-boot-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-logging\2.4.1\spring-boot-starter-logging-2.4.1.jar;F:\work\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\work\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\work\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;F:\work\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;F:\work\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;F:\work\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;F:\work\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;F:\work\repository\org\springframework\spring-core\5.3.2\spring-core-5.3.2.jar;F:\work\repository\org\springframework\spring-jcl\5.3.2\spring-jcl-5.3.2.jar;F:\work\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-json\2.4.1\spring-boot-starter-json-2.4.1.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.1\spring-boot-starter-tomcat-2.4.1.jar;F:\work\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;F:\work\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;F:\work\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;F:\work\repository\org\springframework\spring-web\5.3.2\spring-web-5.3.2.jar;F:\work\repository\org\springframework\spring-beans\5.3.2\spring-beans-5.3.2.jar;F:\work\repository\org\springframework\spring-webmvc\5.3.2\spring-webmvc-5.3.2.jar;F:\work\repository\org\springframework\spring-aop\5.3.2\spring-aop-5.3.2.jar;F:\work\repository\org\springframework\spring-context\5.3.2\spring-context-5.3.2.jar;F:\work\repository\org\springframework\spring-expression\5.3.2\spring-expression-5.3.2.jar;F:\work\workspace-demo\email-springboot-starter\target\classes;F:\work\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.1\spring-boot-autoconfigure-2.4.1.jar;F:\work\repository\com\alibaba\fastjson\1.2.73\fastjson-1.2.73.jar" email.Application

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2022-03-05 10:22:26.041  INFO 3552 --- [           main] email.Application                        : Starting Application using Java 1.8.0_192 on PC-20220219NVTS with PID 3552 (F:\work\workspace-demo\email-springboot-test\target\classes started by MAO in F:\work\workspace-demo)
2022-03-05 10:22:26.041  INFO 3552 --- [           main] email.Application                        : No active profile set, falling back to default profiles: default
2022-03-05 10:22:26.682  INFO 3552 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-03-05 10:22:26.697  INFO 3552 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-03-05 10:22:26.697  INFO 3552 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2022-03-05 10:22:26.760  INFO 3552 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-03-05 10:22:26.760  INFO 3552 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 688 ms
2022-03-05 10:22:26.916  INFO 3552 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-03-05 10:22:27.041  INFO 3552 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-05 10:22:27.041  INFO 3552 --- [           main] email.Application                        : Started Application in 1.36 seconds (JVM running for 2.26)
EmailProperties.MailConfig(userName=AwsSes, password=admin, sender=mao2080@sina.com) SendEmail->message:Hello World !
EmailProperties.MailConfig(userName=SendGrid, password=admin, sender=mao2080@sina.com) SendEmail->message:Hello World !

13、源码下载

        https://github.com/mao2080/email-springboot-starter

3、参考网站

       https://www.cnblogs.com/hello-shf/p/10864977.html

  https://blog.csdn.net/qq_37488998/article/details/112124662

       https://www.cnblogs.com/selfchange/p/10014312.html

posted @ 2022-03-05 10:48  mao2080  阅读(133)  评论(0编辑  收藏  举报