接口

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. pandas 判断列是否包含某个字符串

    亲测第二种好用 in 语句 不包含使用not in food = df['日期'].values.tolist() if '休息' in food: print(food) if df['共计小时'] ...

  2. Jupyter Notebook的所有文件ipynb保存下来

    前言 如果你想要保存整个 Jupyter Notebook 工作目录,包括所有笔记本和其他相关文件,最直接的方法是将整个文件夹压缩为一个 ZIP 或 TAR 文件. 下载单个文件 压缩文件夹下载 在 ...

  3. 因为Apifox不支持离线,我果断选择了Apipost!

    要说国内最有名的两款API开发工具不是Apipost就是Apifox,因为曾经遭遇到一件事,导致我坚定的选择了Apipost. 有一年春节我攒了足够的年假,提前开开心心的过年回家,路上我的领导给我打电 ...

  4. SpringBoot的yml文件报org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here in 'reader', line 11, column 16:

    报错内容如下: 报错代码如下: 1 spring: 2 cloud: 3 gateway: 4 routes: 5 - id: query_route 6 uri: https://baidu.org ...

  5. SpringBoot应用调用Linkis进行任务调度执行SQl;进行数据质量分析

    基于Linkis的Rest-API调用任务 官网示例:"https://linkis.apache.org/zh-CN/docs/1.3.2/api/linkis-task-operator ...

  6. SDF矩形(附圆角)公式推导

    SDF矩形(附圆角)公式推导 矩形 一般情况下,我们会使用(top_left, top_bottom), (width, height)来定义一个矩形,但是对于SDF而言,使用(centerX, ce ...

  7. 阅读IDEA生成的equals方法--java进阶day05

    1.IDEA生成的equals方法 虽然我们之前写了equals方法,但IDEA中可以快速生成equals方法,因此,我们要能看懂IDEA生成的equals方法 1.if(this==o) 2.if( ...

  8. Microsoft.NETCore.App 版本不一致导致的运行失败

    场景重现 今天新建了一个 ASP.NET Core 的项目, 通过 Web Deploy 顺利发布到IIS上后, 但访问时出现如下异常: 异常原因 通过手动执行dotnet命令发现运行框架版本不一致? ...

  9. 记录一次ubuntu软件安装未完全的解决

    背景 预想是在ubuntu20.10上去安装android-studio,所以找了个教程,是使用ubuntu-make来进行安装,不过我也不知为何,安装到最后,出现了dpkg的报错并返回,错误提示是让 ...

  10. expected at least 1 bean which qualifies as autowire candidate

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'log ...