当前位置:网站首页>整合阿里云短信的问题:无法从静态上下文中引用非静态方法
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
2022-07-01 03:21:00 【家家小迷弟】
在做阿里云短信整合的时候,签名始终不通过,这里使用测试的API:
视频中用的应该是原来的SDK,这里我就是用新版的:
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.*;
import com.aliyun.teautil.models.*;
public class Sample {
/** * 使用AK&SK初始化账号Client * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dysmsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers("17683839612")
.setTemplateParam("{\"code\":\"1234\"}");
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
这里我把初始化的createClient抽成一个工具类:AliyunUtils:
这里先不使用静态方法:
@Component
public class AliyunUtils implements InitializingBean {
@Value("${aliyun.msg.file.keyid}")
private String accessKeyId;
@Value("${aliyun.msg.file.keysecret}")
private String accessKeySecret;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID=accessKeyId;
ACCESS_KEY_SECRET=accessKeySecret;
}
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(ACCESS_KEY_ID)
// 您的 AccessKey Secret
.setAccessKeySecret(ACCESS_KEY_SECRET);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
在实现类中:AliyunUtils 是通过new对象实例调用的,
@Override
public boolean send(Map<String, Object> param, String phone) throws Exception {
if (StrUtil.isEmpty(phone)){
return false;
}
AliyunUtils aliyunUtils=new AliyunUtils();
//java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dysmsapi20170525.Client client = aliyunUtils.createClient();
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers("176xxxxxxxx")
.setTemplateParam("{\"code\": "+ param.get("code")+"}");
/*.setTemplateParam("{\"code\":\"1234\"}");*/
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
return true;
}
}
下面是静态方法的方式调用:将createClient定义为静态方法,注意的是:
这种方式将取不到值,会报错: 无法从静态上下文中引用非静态方法
@Value("${aliyun.msg.file.keysecret}")
private String accessKeySecret;
普通变量在使用@value注解时,直接在变量上加 @value 注解,并通过 spring表达式写上想要获取的值,就可以将配置值注入到变量中。但如果是静态变量就无法这样注入,那么静态变量如何通过 @value 注解进行注入,这和spring 注入静态变量相似,需要写一个set方法,spring 通过调用set方法进行注入,并将注入值赋值给静态变量
@Component
public class AliyunUtils {
@Value("${aliyun.msg.file.keyid}")
public void setKey(String key) {
AliyunUtils.ACCESS_KEY_ID = key;
}
@Value("${aliyun.msg.file.keysecret}")
public void setSecret(String secret) {
AliyunUtils.ACCESS_KEY_SECRET = secret;
}
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(ACCESS_KEY_ID)
// 您的 AccessKey Secret
.setAccessKeySecret(ACCESS_KEY_SECRET);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
这里直接调用静态方法
两种方法实际上都是取配置文件的key和secret,并赋值给静态变量
边栏推荐
- Avalanche problem and the use of sentinel
- You cannot right-click F12 to view the source code solution on the web page
- torch.histc
- Hal library setting STM32 interrupt
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- 文件上传下载
- Ctfshow blasting WP
- Hal library operation STM32 serial port
- gcc使用、Makefile总结
- Kmeans
猜你喜欢

Leetcode 128 longest continuous sequence (hash set)

FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)

Introduction to EtherCAT

4、【WebGIS实战】软件操作篇——数据导入及处理

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)

Keil5中如何做到 0 Error(s), 0 Warning(s).
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

服务器渲染技术jsp

排序链表(归并排序)

Bilinear upsampling and f.upsample in pytorch_ bilinear
随机推荐
Include() of array
torch.histc
Leetcode: offer 59 - I. maximum value of sliding window
多元线性回归
leetcode 1818 绝对值,排序,二分法,最大值
Avalanche problem and the use of sentinel
线程数据共享和安全 -ThreadLocal
Edge Drawing: A combined real-time edge and segment detector 翻译
完全背包问题
排序链表(归并排序)
Detailed explanation of ES6 deconstruction grammar
串口接收数据方案设计
Go tool cli for command line implementation
静态库使用MFC和共享库使用MFC的区别
The shell script uses two bars to receive external parameters
Server rendering technology JSP
Pathmeasure implements loading animation
岭回归和lasso回归
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动