转载

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

接口

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);
正文到此结束
Loading...