首页 > 编程语言 >java实现一个接口多个实现类,并且调用指定实现方法@Service

java实现一个接口多个实现类,并且调用指定实现方法@Service

时间:2023-09-26 14:00:12浏览次数:38  
标签:info java cn Service 实现 param SmsSendResult import com

接口

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);

标签:info,java,cn,Service,实现,param,SmsSendResult,import,com
From: https://www.cnblogs.com/daen/p/17729941.html

相关文章

  • springboot大文件上传、分片上传、断点续传、秒传的实现
    对于大文件的处理,无论是用户端还是服务端,如果一次性进行读取发送、接收都是不可取,很容易导致内存问题。所以对于大文件上传,采用切块分段上传,从上传的效率来看,利用多线程并发上传能够达到最大效率。 本文是基于springboot+vue实现的文件上传,本文主要介绍服务端实现文件上传的......
  • 解析es6中let和const并模拟实现私有变量
    使用let和const声明变量早已经习以为常了。笔者作为面试官面试过上百人,能准确理解let/const块级作用域以及的候选人不足一二。本文将深入研究let和const的实现原理,以及多种方式来模拟私有变量,希望本文能给初中级前端小伙伴们一点帮助。一、let和const的实现原理1.1......
  • Service mesh 学习03 Istio安装
    一、Kubernetes环境......
  • 浅析实现大文件上传和断点续传
    大文件上传:前端部分:核心是利用 Blob.prototype.slice 方法,和数组的slice方法相似,调用的slice方法可以返回原文件的某个切片。根据预先设置好的切片最大数量将文件切分为一个个切片,然后借助http的可并发性,同时上传多个切片。这样从原本传一个大文件,变成了同时传多个小......
  • 使用 Spring Integration 实现基于 Redis 的分布式锁以及踩坑
    背景分布式锁的应用场景应该还是蛮多的,这里就不赘述了。之前在开发中实现分布式锁都是自己基于Redis造轮子,虽然也不复杂并且自己实现一次能对分布式锁有更深的了解,但是终归有些麻烦。尤其是新项目需要的时候还得CV一次。然后在查询过程中(毫不意外地)发现Spring有现成的组......
  • 解锁Java面试中的锁:深入了解不同类型的锁和它们的用途
    简介多线程编程在现代软件开发中扮演着至关重要的角色。它使我们能够有效地利用多核处理器和提高应用程序的性能。然而,多线程编程也伴随着一系列挑战,其中最重要的之一就是处理共享资源的线程安全性。在这个领域,锁(Lock)是一个关键的概念,用于协调线程之间对共享资源的访问。本文将深......
  • ModBus协议原理、Modbus Slave以及基于C++和Qt的代码实现
    ModBus协议目的:规定与PLC交互的指令,其数据帧包括两部分:报文头(MBAP)和帧结构(PDU)。报文头(MBAP)(分为6个部分):1.事务处理标识:即报文序列号,一般每次通信之后就要加1以区别不同的通信数据报文,长度2字节。2.协议标识符:有串口的RTU协议和TCP协议,如0000表示ModbusTCP......
  • 80基于java在线婚纱定制系统设计与实现(含配套lun文,可参考做bi设)
    本章节给大家带来一个基于java在线婚纱定制系统设计与实现,可适用于在线婚纱摄影预定系统,基于web的婚纱影楼管理系统设计,基于web的婚纱影楼管理系统设计,婚纱摄影网系统,婚纱摄影网站系统,婚纱摄影网站系统,婚纱系统,婚纱管理系统等等;项目背景一件完美的婚纱相当于一件艺术品,婚纱设......
  • 基于JavaWeb的家居商城系统的设计与实现
    背景及意义系统管理也都将通过计算机进行整体智能化操作,对于家居商城系统所牵扯的管理及数据保存都是非常多的,例如管理员;主页、个人中心、用户管理、商品分类管理、商品信息管理、系统管理、订单管理,用户;主页、个人中心、我的收藏管理、订单管理,前台首页;商品信息、新闻资讯、我的、......
  • 如何在Python中实现高效的数据处理与分析
    在当今信息爆炸的时代,我们面对的数据量越来越大,如何高效地处理和分析数据成为了一种迫切的需求。Python作为一种强大的编程语言,提供了丰富的数据处理和分析库,帮助我们轻松应对这个挑战。本文将为您介绍如何在Python中实现高效的数据处理与分析,以提升工作效率和数据洞察力。1、数据......