当前位置:网站首页>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
边栏推荐
- 在线公网安备案保姆级教程【伸手党福利】
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- 72. 编辑距离
- Develop industrial Internet with the technical advantages of small programs
- You cannot right-click F12 to view the source code solution on the web page
- [nine day training] content III of the problem solution of leetcode question brushing Report
- 【TA-霜狼_may-《百人计划》】2.1 色彩空间
- 详解Spark运行模式(local+standalone+yarn)
- Complete knapsack problem
- 392. 判断子序列
猜你喜欢
Review column - message queue
Thread data sharing and security -threadlocal
pytorch训练深度学习网络设置cuda指定的GPU可见
Binary tree god level traversal: Morris traversal
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
How keil displays Chinese annotations (simple with pictures)
Use of comment keyword in database
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
随机推荐
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
241. 为运算表达式设计优先级
What happens when a function is called before it is declared in C?
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
5. [WebGIS practice] software operation - service release and permission management
idea插件备份表
How keil displays Chinese annotations (simple with pictures)
Valentine's Day is nothing.
10. regular expression matching
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
pytorch nn. AdaptiveAvgPool2d(1)
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
The difference between MFC for static libraries and MFC for shared libraries
MFC窗口滚动条用法
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
168. Excel表列名称
392. judgment subsequence