SpringBoot自定义启动器

1.starter的作用

1.1 starter的理念

starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦。 需要注意的是 不同的starter是为了解决不同的依赖,所以它们内部的实现可能会有很大的差异, 例如jpa的starter和Redis的starter可能实现就不一样,这是因为starter的本质在于synthesize, 这是一 层在逻辑层面的抽象,也许这种理念有点类似于Docker,因为它们都是在做一个“包装”的操作。

1.2 starter的实现

虽然不同的starter实现起来各有差异, 但是他们基本上都会使用到两个相同的内容: ConfigurationProperties和AutoConfiguration。 因为Spring Boot坚信“约定大于配置”这一理念,所以我们使用ConfigurationProperties来保存我们的配置, 并且这 些配置都可以有一个默认值,即在我们没有主动覆写原始配置的情况下,默认值就会生效,这在很多情 况下是非常有用的。 除此之外,starter的ConfigurationProperties还使得所有的配置属性被聚集到一个文件中 (一般在 resources目录下的application.properties),这样我们就告别了Spring项目中XML地狱。
 

2 .starter 制作( java操作邮件实例)

2.1导入pom依赖

<dependency>  
  <groupId>org.springframework.boot</groupId>  
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

 2.2 定义实体类注入

@ConfigurationProperties 作用:根据属性完成yml注入(需要bean对象被spring容器管理) 

package com.wyy.springbootstarter;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "mail")
public class EmailProperties {

    /**
     * SMTP服务器
     */
    private String host;

    /**
     *邮箱账号
     */
    private String username;

    /**
     *授权密码
     */
    private String password ;

    /**
     * 是否开启
     */
    private boolean enable;
}

2.3 定义操作接口

package com.wyy.springbootstarter;

public interface EmailSender {
   

    String sendText(String receiver,String title,String text);
  
}

2.4 接口实现

@ConfigurationProperties 会让元素加载配置文件中的内容,但是需要成为组件才能实现内容的读取
通过在类中加上 @component 注解使其自动加载

package com.wyy.springbootstarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Configuration
@ConditionalOnProperty(prefix = "mail",name = "enable",havingValue = "true")
public class EmailSenderImpl implements EmailSender {


    private EmailProperties emailProperties;
    private  JavaMailSenderImpl  mailSender;
    @Autowired
    EmailSenderImpl(EmailProperties emailPropertie){
        this.emailProperties=emailPropertie;
        mailSender = new JavaMailSenderImpl() ;
        mailSender.setHost(emailProperties.getHost());
        mailSender.setUsername(emailProperties.getUsername());
        mailSender.setPassword(emailProperties.getPassword());
        mailSender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.starttls.enable", "true");
        p.setProperty("mail.smtp.starttls.required", "true");
        mailSender.setJavaMailProperties(p);
    }

    @Override
    public String sendText(String receiver,String title,String text) {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(emailProperties.getUsername());
        message.setTo(receiver);
        message.setSubject(title);
        message.setText(text);
        mailSender.send(message);
        return "ad";
    }

   
}

2.5其他配置

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

在resources目录下创建META-INF文件夹,然后添加文件 spring.factories。注意:实现类更改为自己实现类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wyy.springbootstarter.EmailSenderImpl

注册到本地
  

 3.starter使用

3.1导入自定义starter

<dependency>
            <groupId>com.wyy</groupId>
            <artifactId>yzm-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

3.2yml属性注入 

这里以163邮箱为例

mail:
  host: smtp.163.com  
  password: **********    
  username: ********@163.com    
  enable: true

测试

package com.wyy.wy;

import com.wyy.springbootstarter.EmailSender;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@SpringBootTest
class SpringBoot06ApplicationTests {

    @Autowired
    EmailSender emailSender;
    @Test
    void contextLoads() {
        emailSender.sendText("513473567@qq.com","欠费通知","你已欠费");

        }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值