当前位置:网站首页>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
边栏推荐
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- 241. 为运算表达式设计优先级
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- 205. isomorphic string
- FCN全卷积网络理解及代码实现(来自pytorch官方实现)
- LeetCode 128最长连续序列(哈希set)
- 392. judgment subsequence
- TEC: Knowledge Graph Embedding with Triple Context
猜你喜欢

Blueprism registration, download and install -rpa Chapter 1

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

深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)

Complete knapsack problem

Take you through a circuit board, from design to production (dry goods)

Server rendering technology JSP

pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
随机推荐
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
205. 同构字符串
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
Future of NTF and trends in 2022
[daily training] 1175 Prime permutation
Online public network security case nanny level tutorial [reaching out for Party welfare]
171. excel table column No
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
208. 实现 Trie (前缀树)
8. 字符串转换整数 (atoi)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
165. compare version numbers
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
318. 最大单词长度乘积
[reach out to Party welfare] developer reload system sequence
【快捷键】
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Feature pyramid networks for object detection