封装短信发送服务组件
企业开发中,往往将常见工具类封装抽取,以简洁便利的方式供其他工程模块使用。而SpringBoot的自动装配机制可以方便的实现组件抽取。SpringBoot执行流程如下
- 扫描依赖模块中META-INF/spring.factories
- 执行装配类中方法
- 对象存入容器中
- 核心工程注入对象,调用方法使用
1.发送短信模板对象
在tanhua-autoconfig模块中建一个com.tanhua.autoconfig.template的包上新建一个
SmsTemplate发送短信模板的类,里面是发送手机验证码短信的代码
package com.tanhua.autoconfig.template;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
/**
* 手机验证码发送模板
*/
public class SmsTemplate {
public void sendSms(String mobile, String code) {
String accessKeyId ="LTAI4GKgob9vZ53k2SZdyAC7";
String accessKeySecret = "LHLBvXmILRoyw0niRSBuXBZewQ30la";
try {
//配置阿里云
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(mobile)
.setSignName("物流云商")
.setTemplateCode("SMS_106590012")
.setTemplateParam("{\"code\":\""+code+"\"}");
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse response = client.sendSms(sendSmsRequest);
SendSmsResponseBody body = response.getBody();
System.out.println(body.getMessage());
}catch (Exception e) {
e.printStackTrace();
}
}
}
2.创建配置类加载短信模板对象到spring容器中
在com.tanhua.autoconfig.创建TanhuaAutoConfiguration配置类
package com.tanhua.autoconfig;
import com.tanhua.autoconfig.properties.SmsProperties;
import com.tanhua.autoconfig.template.SmsTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* 我愿称它为配置类
*/
public class TanhuaAutoConfiguration {
/**
*
* @return 加载手机验证码发送模板类到spring容器中
*/
@Bean
public SmsTemplate smsTemplate() {
return new SmsTemplate();
}
}
3.根据自动装配原则,在tanhua-autoconfig工程创建 /META-INF/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tanhua.autoconfig.TanhuaAutoConfiguration
4.测试,在tanhua-app-server模块中引入tanhua-autoconfig依赖,在测试模块建一个测试类,把短信模板对象以springBoot自动装配注入
package com.itheima.test;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.server.AppServerApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class SmsTemplateTest {
//注入
@Autowired(required = false)
private SmsTemplate smsTemplate;
//测试
@Test
public void testSendSms() {
smsTemplate.sendSms("18177760987","4567");
}
}
5.模板对象中的发送手机验证码的参数是以硬编码的方式;我们要改良一下代码,以application.yml的方式配置;再读取配置
5.1在tanhua-app-server模块中的application.yml配置文件中加入短信配置
tanhua:
sms:
signName: 物流云商
templateCode: SMS_106590012
accessKey: LTAI4GKgob9vZ53k2SZdyAC7
secret: LHLBvXmILRoyw0niRSBuXBZewQ30la
5.2在tanhua-autoconfig模块中建一个properties包中建一个配置信息类,属性与短信信息一一对应
package com.tanhua.autoconfig.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 读取application.yml配置文件的类,与配置文件的属性一一对应
*/
@Data
@ConfigurationProperties(prefix = "tanhua.sms")
public class SmsProperties {
private String signName;
private String templateCode;
private String accessKey;
private String secret;
}
5.3在com/tanhua/autoconfig/TanhuaAutoConfiguration.java配置类中自动注入SmsProperties
package com.tanhua.autoconfig;
import com.tanhua.autoconfig.properties.SmsProperties;
import com.tanhua.autoconfig.template.SmsTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* 我愿称它为配置类
*/
@EnableConfigurationProperties({SmsProperties.class})
public class TanhuaAutoConfiguration {
/**
*
* @return 加载手机验证码发送模板类到spring容器中
*/
@Bean
public SmsTemplate smsTemplate(SmsProperties smsProperties) {
return new SmsTemplate(smsProperties);
}
}
5.4在短信模板对象中以构造方法的形式把短信信息配置类SmsProperties注入,调用属性就是读取application.yml的配置信息
package com.tanhua.autoconfig.template;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.tanhua.autoconfig.properties.SmsProperties;
/**
* 手机验证码发送模板
*/
public class SmsTemplate {
//通过构造方法注入依赖
private SmsProperties smsProperties;
public SmsTemplate(SmsProperties smsProperties) {
this.smsProperties = smsProperties;
}
public void sendSms(String mobile, String code) {
try {
//配置阿里云
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(smsProperties.getAccessKey())
// 您的AccessKey Secret
.setAccessKeySecret(smsProperties.getSecret());
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(mobile)
.setSignName(smsProperties.getSignName())
.setTemplateCode(smsProperties.getTemplateCode())
.setTemplateParam("{\"code\":\""+code+"\"}");
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse response = client.sendSms(sendSmsRequest);
SendSmsResponseBody body = response.getBody();
System.out.println(body.getMessage());
}catch (Exception e) {
e.printStackTrace();
}
}
}
标签:短信,public,tanhua,aliyun,import,组件,封装,com,autoconfig
From: https://www.cnblogs.com/zhangdashuaige/p/16840869.html