当前位置:网站首页>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
边栏推荐
- Complete knapsack problem
- 187. 重复的DNA序列
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- 318. Maximum word length product
- Download and installation configuration of cygwin
- 详解Spark运行模式(local+standalone+yarn)
- [shortcut key]
- Listener listener
- 5、【WebGIS实战】软件操作篇——服务发布及权限管理
- 166. 分数到小数
猜你喜欢

Ultimate dolls 2.0 | encapsulation of cloud native delivery

Explain spark operation mode in detail (local+standalone+yarn)

Feature Pyramid Networks for Object Detection论文理解

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

数据库中COMMENT关键字的使用
![4. [WebGIS practice] software operation chapter - data import and processing](/img/5a/b86e0538660f27c809cf429053a06c.png)
4. [WebGIS practice] software operation chapter - data import and processing

Idea plug-in backup table

静态库使用MFC和共享库使用MFC的区别

Appium fundamentals of automated testing - basic principles of appium

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
随机推荐
30. Concatenate substrings of all words
You cannot right-click F12 to view the source code solution on the web page
392. 判断子序列
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
[daily training] 1175 Prime permutation
pytorch训练深度学习网络设置cuda指定的GPU可见
Ultimate dolls 2.0 | encapsulation of cloud native delivery
205. isomorphic string
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
[party benefits] jsonobject to string, leave blank
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
MFC窗口滚动条用法
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
整合阿里云短信的问题:无法从静态上下文中引用非静态方法
171. Excel 表列序号
How keil displays Chinese annotations (simple with pictures)
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
Complete knapsack problem
214. 最短回文串