在Spring Boot中,可以使用工厂类来实现阿里云、腾讯云和华为云的短信发送功能。首先,需要创建一个接口,定义发送短信的通用方法,然后为每个云服务提供具体的实现,最后创建一个工厂类来根据配置选择正确的实现。
在Spring Boot中,可以使用工厂类来实现阿里云、腾讯云和华为云的短信发送功能。首先,需要创建一个接口,定义发送短信的通用方法,然后为每个云服务提供具体的实现,最后创建一个工厂类来根据配置选择正确的实现。以下是一个简单的示例:
创建一个通用的短信发送接口:
public interface SmsSender { void sendSms(String phoneNumber, String message);}1.2.3.
创建具体的短信发送服务实现类,分别为阿里云、腾讯云和华为云创建实现类。例如,以下是阿里云的实现:
@Servicepublic class AliyunSmsSender implements SmsSender { private final String accessKeyId; private final String accessKeySecret; private final String signName; @Autowired public AliyunSmsSender( @Value("${sms.aliyun.accessKeyId}") String accessKeyId, @Value("${sms.aliyun.accessKeySecret}") String accessKeySecret, @Value("${sms.aliyun.signName}") String signName ) { this.accessKeyId = accessKeyId; this.accessKeySecret = accessKeySecret; this.signName = signName; } @Override public void sendSms(String phoneNumber, String message) { // 使用阿里云的SDK发送短信 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); // 设置短信发送参数 request.putQueryParameter("PhoneNumbers", phoneNumber); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", "your_template_code"); request.putQueryParameter("TemplateParam", "{\"code\":\"" + message + "\"}"); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.
腾讯云的实现类
@Servicepublic class TencentSmsSender implements SmsSender { private final String appId; private final String appKey; private final String sign; @Autowired public TencentSmsSender( @Value("${sms.tencent.appId}") String appId, @Value("${sms.tencent.appKey}") String appKey, @Value("${sms.tencent.sign}") String sign ) { this.appId = appId; this.appKey = appKey; this.sign = sign; } @Override public void sendSms(String phoneNumber, String message) { // 使用腾讯云的SDK发送短信 // 实现具体的发送逻辑,参考腾讯云短信SDK文档 } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
华为云的实现类
@Servicepublic class HuaweiSmsSender implements SmsSender { private final String clientId; private final String clientSecret; private final String sender; @Autowired public HuaweiSmsSender( @Value("${sms.huawei.clientId}") String clientId, @Value("${sms.huawei.clientSecret}") String clientSecret, @Value("${sms.huawei.sender}") String sender ) { this.clientId = clientId; this.clientSecret = clientSecret; this.sender = sender; } @Override public void sendSms(String phoneNumber, String message) { // 使用华为云的SDK发送短信 // 实现具体的发送逻辑,参考华为云短信SDK文档 } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.
创建一个工厂类,根据配置选择正确的短信发送实现类:
@Servicepublic class SmsSenderFactory { private final AliyunSmsSender aliyunSmsSender; private final TencentSmsSender tencentSmsSender; private final HuaweiSmsSender huaweiSmsSender; @Autowired public SmsSenderFactory(AliyunSmsSender aliyunSmsSender, TencentSmsSender tencentSmsSender, HuaweiSmsSender huaweiSmsSender) { this.aliyunSmsSender = aliyunSmsSender; this.tencentSmsSender = tencentSmsSender; this.huaweiSmsSender = huaweiSmsSender; } public SmsSender getSmsSender(SmsProvider provider) { switch (provider) { case ALIYUN: return aliyunSmsSender; case TENCENT: return tencentSmsSender; case HUAWEI: return huaweiSmsSender; default: throw new IllegalArgumentException("Unsupported SMS provider: " + provider); } } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.
创建一个枚举类来表示短信服务提供商:
public enum SmsProvider { ALIYUN, TENCENT, HUAWEI }1.2.3.4.5.
在Spring Boot应用中,配置并注入工厂类,根据配置选择正确的短信服务提供商:
# application.properties 中的配置sms.provider=ALIYUN1.2.
在服务或控制器中,可以注入SmsSenderFactory,然后根据配置选择合适的短信服务提供商,并使用工厂创建短信发送实例进行发送:
@Servicepublic class SmsService { private final SmsSenderFactory smsSenderFactory; @Value("${sms.provider}") private SmsProvider smsProvider; @Autowired public SmsService(SmsSenderFactory smsSenderFactory) { this.smsSenderFactory = smsSenderFactory; } public void sendSms(String phoneNumber, String message) { SmsSender smsSender = smsSenderFactory.getSmsSender(smsProvider); smsSender.sendSms(phoneNumber, message); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.
这种方式使您能够轻松切换不同的短信服务提供商,并根据配置选择合适的实现类发送短信。请确保您已在项目中引入相应的云服务SDK,并配置好相关的参数,以便实际发送短信。
示例中完整代码,可以从下面网址获取:
https://gitee.com/jlearning/wechatdemo.git
https://github.com/icoderoad/wxdemo.git
责任编辑:武晓燕来源: 路条编程