接口

package cn.daenx.framework.notify.sms.service;

import cn.daenx.framework.common.vo.system.utils.SmsSendResult;

import java.util.Map;

/**
* 短信接口
*/
public interface SmsService {
SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param);
}

阿里云实现

package cn.daenx.framework.notify.sms.service.impl;

import cn.daenx.framework.common.vo.system.utils.SmsSendResult;
import cn.daenx.framework.notify.sms.service.SmsService;
import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Service; import java.util.Map; @Service("aliyun")
public class AliyunSmsService implements SmsService {
/**
* 发送短信协议,阿里云
*
* @param info
* @param phones 多个手机号用,隔开
* @param templateId
* @param param 例如模板为:您的验证码为:${code},请勿泄露于他人!那么key=code,value=1234
* @return
*/
@Override
public SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param) {
try {
Config config = new Config();
config.setAccessKeyId(info.get("accessKeyId"));
config.setAccessKeySecret(info.get("accessKeySecret"));
config.setEndpoint(info.get("endpoint"));
Client client = new Client(config);
com.aliyun.dysmsapi20170525.models.SendSmsRequest req = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setPhoneNumbers(phones)
.setSignName(info.get("signName"))
.setTemplateCode(templateId)
.setTemplateParam(JSON.toJSONString(param));
SendSmsResponse resp = client.sendSms(req);
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess("OK".equals(resp.getBody().getCode())).msg(resp.getBody().getMessage()).aliyunRes(resp).build();
return smsSendResult;
} catch (Exception e) {
e.printStackTrace();
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(false).msg(e.getMessage()).build();
return smsSendResult;
}
}
}

腾讯云实现

package cn.daenx.framework.notify.sms.service.impl;

import cn.daenx.framework.common.vo.system.utils.SmsSendResult;
import cn.daenx.framework.notify.sms.service.SmsService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.stereotype.Service; import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; @Service("tencent")
public class TencentSmsService implements SmsService {
/**
* 发送短信协议,腾讯云
*
* @param info
* @param phones 多个手机号用,隔开,需要加+86等前缀
* @param templateId
* @param param 例如模板为:例如模板为:验证码为:{1},有效期为{2}分钟,如非本人操作,请忽略本短信。那么key=1,value=6666,key=2,value=5
* @return
*/
@Override
public SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param) {
try {
Credential credential = new Credential(info.get("accessKeyId"), info.get("accessKeySecret"));
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(info.get("endpoint"));
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(credential, "", clientProfile);
com.tencentcloudapi.sms.v20190711.models.SendSmsRequest req = new SendSmsRequest();
Set<String> set = Arrays.stream(phones.split(",")).collect(Collectors.toSet());
req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));
if (CollUtil.isNotEmpty(param)) {
req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));
}
req.setTemplateID(templateId);
req.setSign(info.get("signName"));
req.setSmsSdkAppid(info.get("sdkAppId"));
com.tencentcloudapi.sms.v20190711.models.SendSmsResponse resp = client.SendSms(req);
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(true).msg("ok").tencentRes(resp).build();
for (SendStatus sendStatus : resp.getSendStatusSet()) {
if (!"Ok".equals(sendStatus.getCode())) {
smsSendResult.setSuccess(false);
sendStatus.setMessage(sendStatus.getMessage());
break;
}
}
return smsSendResult;
} catch (Exception e) {
e.printStackTrace();
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(false).msg(e.getMessage()).build();
return smsSendResult;
}
}
}

调用

//type就是实现类方法上的@Service注解的参数
//String type = "tencent";
String type = "aliyun";
//SpringUtil是hutool里的工具类
SmsService smsService = SpringUtil.getApplicationContext().getBean(type, SmsService.class);
SmsSendResult smsSendResult = smsService.sendSms(xxx, xxx, xxx, xxx);

java实现一个接口多个实现类,并且调用指定实现方法@Service的更多相关文章

  1. net core天马行空系列: 一个接口多个实现类,利用mixin技术通过自定义服务名,实现精准属性注入

    系列目录 1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程 2.net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作 哈哈哈哈,大家好,我 ...

  2. DevOps运动的缘起 将DevOps想象为一种编程语言里面的一个接口,而SRE类实现了这个接口

     SRE vs DevOps:是敌是友? - DockOne.io http://www.dockone.io/article/5935   RE vs DevOps:是敌是友? [编者的话]网站可靠 ...

  3. 反射工具类.提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class,被AOP过的真实类等工具函数.java

    import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.ap ...

  4. java 写一个JSON解析的工具类

    上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...

  5. Java -- 获取指定接口的所有实现类或获取指定类的所有继承类

    Class : ClassUtil package pri.lime.main; import java.io.File; import java.io.IOException; import jav ...

  6. 如何得到一个接口所有的实现类(及子接口)?例如:Eclipse IDE

    (一)Eclipse IDE的做法 它会解析所有的Java文件.Class文件. 技巧:在Eclipse中,选中Interface,按下F4,就可以查看到所有的实现类及子接口. 例如: (二)自己怎么 ...

  7. java new一个接口到底要做什么

    转自:http://www.cnblogs.com/yjmyzz/p/3448330.html java中的匿名类有一个倍儿神奇的用法,见下面代码示例: 1 package contract; 2 3 ...

  8. SSM(Spring)中,在工具类中调用服务层的方法

    因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...

  9. 一个接口多个实现类的Spring注入方式

    1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...

  10. Effective Java:Ch4_Class:Item14_在public类中应该使用访问方法而不是public域

    你可能偶尔需要编写退化类,目的只是为了集中实例域: // Degenerate classes like this should not be public! class Point { public ...

随机推荐

  1. gitee如何删除仓库

    进入仓库的管理页面点击删除

  2. 【Unit1】表达式化简(层次化设计)-作业总结

    三次作业围绕表达式化简展开,逐次递进.主体思路为:递归下降解析表达式保存至类中,依据相关模式化简,依照规范输出字符串. 1.第一次作业 1.1 题目概述 表达式 = 项 + 项 + ... 项 = 因 ...

  3. 服务器Go程序意外停止自动重启

    判断进程是否挂掉 ps -ef | grep ./blog |wc -l 如果输出为1,说明进程挂掉了 如果输出为2,说明进程正常运行 编辑脚本来检测和完成重启 vim restart.sh 逻辑代码 ...

  4. Linux基础知识之:crontab定时任务

    目录 5.3 定时(计划)任务crontab 5.3.1 定时任务的概念 5.3.2 定时任务的作用 5.3.3 crontab命令语法 5.3.4. crontab编辑语法 5.4.5 定时任务的编 ...

  5. Efficient Scalable Multi-Party Private Set Intersection

    论文学习:Efficient Scalable Multi-Party Private Set Intersection 这篇论文提出了一种基于双中心零共享(Bicentric Zero-Sharin ...

  6. AI可解释性 I | 对抗样本(Adversarial Sample)论文导读(持续更新)

    AI可解释性 I | 对抗样本(Adversarial Sample)论文导读(持续更新) 导言 本文作为AI可解释性系列的第一部分,旨在以汉语整理并阅读对抗攻击(Adversarial Attack ...

  7. MCP技术:渗透测试从自动化到智能化

    在人工智能快速发展的今天,如何让AI更高效地与现实世界交互,成为许多开发者和研究者关注的焦点.MCP(Model Context Protocol)技术作为一种创新的工具集成方案,为AI提供了一种&q ...

  8. awesome llm web ui

    https://github.com/JShollaj/awesome-llm-web-ui Contents Streamlit Gradio Lobe Chat Text Generation W ...

  9. spring_声明式事务: @Transactional

    例子. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  10. 原型设计工具Axure墨刀哪个好用?

    一.工具基础特性对比 Axure为本地化安装软件,支持离线操作且数据存储本地,安全性较高,但多端协作需通过云端同步,存在更新延迟:墨刀则为云端在线工具,通过浏览器即可使用,无需安装,便于多端协作与实时 ...