当前位置:网站首页>The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
2022-07-01 03:44:00 【My little brother】
When doing Alibaba cloud SMS integration , The signature never passed , Here use the tested API:
The video should be the original SDK, Here I use the new version :
// 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 {
/** * Use AK&SK Initialization account 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()
// Your AccessKey ID
.setAccessKeyId(accessKeyId)
// Your AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// Domain name visited
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(" Alibaba cloud SMS test ")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers("17683839612")
.setTemplateParam("{\"code\":\"1234\"}");
RuntimeOptions runtime = new RuntimeOptions();
try {
// Please print the copy code yourself API The return value of
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
Here I initialize createClient Draw a tool class :AliyunUtils:
Don't use static methods here :
@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()
// Your AccessKey ID
.setAccessKeyId(ACCESS_KEY_ID)
// Your AccessKey Secret
.setAccessKeySecret(ACCESS_KEY_SECRET);
// Domain name visited
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
In the implementation class :AliyunUtils It's through new Object instance called ,
@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(" Alibaba cloud SMS test ")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers("176xxxxxxxx")
.setTemplateParam("{\"code\": "+ param.get("code")+"}");
/*.setTemplateParam("{\"code\":\"1234\"}");*/
RuntimeOptions runtime = new RuntimeOptions();
try {
// Please print the copy code yourself API The return value of
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
}
return true;
}
}
The following is how static methods are called : take createClient Defined as a static method , Pay attention to is :
This method will not get the value , Will report a mistake : Cannot reference a non static method from a static context
@Value("${aliyun.msg.file.keysecret}")
private String accessKeySecret;
Ordinary variables are in use @value When the annotation , Add directly to the variable @value annotation , And pass spring The expression is written with the value you want to get , You can inject configuration values into variables . But if it is a static variable, it cannot be injected in this way , So how do static variables pass @value Annotations are injected , This sum spring Injecting static variables is similar , Need to write a set Method ,spring By calling set Method to inject , And assign the injected value to the static variable
@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()
// Your AccessKey ID
.setAccessKeyId(ACCESS_KEY_ID)
// Your AccessKey Secret
.setAccessKeySecret(ACCESS_KEY_SECRET);
// Domain name visited
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
Here you call the static method directly 
Both methods actually take the configuration file key and secret, And assign it to a static variable
边栏推荐
- Pathmeasure implements loading animation
- [深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
- pytorch nn. AdaptiveAvgPool2d(1)
- 72. edit distance
- [daily training] 1175 Prime permutation
- 小程序容器技术与物联网IoT的结合点
- C语言的sem_t变量类型
- Complete knapsack problem
- The difference between MFC for static libraries and MFC for shared libraries
- The combination of applet container technology and IOT
猜你喜欢

How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars

快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

jeecgboot输出日志,@Slf4j的使用方法

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

Research on target recognition and tracking based on 3D laser point cloud

Review column - message queue

AfxMessageBox和MessageBox的用法
随机推荐
数据库DDL(Data Definition Language,数据定义语言)知识点
Leetcode:829. Sum of continuous integers
torch. histc
The difference between MFC for static libraries and MFC for shared libraries
【TA-霜狼_may-《百人计划》】2.1 色彩空间
[nine day training] content III of the problem solution of leetcode question brushing Report
72. 编辑距离
【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
Download and installation configuration of cygwin
10、Scanner. Next() cannot read spaces /indexof -1
171. Excel 表列序号
242. valid Letter heteronyms
Leetcode: offer 59 - I. maximum value of sliding window
复习专栏之---消息队列
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Pyramid Scene Parsing Network【PSPNet】论文阅读
【快捷键】
Thread data sharing and security -threadlocal
4、【WebGIS实战】软件操作篇——数据导入及处理