当前位置:网站首页>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
边栏推荐
- 5. [WebGIS practice] software operation - service release and permission management
- Test function in pychram
- The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
- 【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
- MFC窗口滚动条用法
- FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
- 30. Concatenate substrings of all words
- 小程序容器技术与物联网IoT的结合点
- Jeecgboot output log, how to use @slf4j
- 数据库DDL(Data Definition Language,数据定义语言)知识点
猜你喜欢

bootsrap中的栅格系统

Pyramid Scene Parsing Network【PSPNet】论文阅读

Idea plug-in backup table

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

Ultimate dolls 2.0 | encapsulation of cloud native delivery

[TA frost wolf \u may - "hundred people plan"] 2.1 color space

Cygwin的下载和安装配置

TEC: Knowledge Graph Embedding with Triple Context

Review column - message queue

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
随机推荐
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
5、【WebGIS实战】软件操作篇——服务发布及权限管理
【快捷键】
Valid brackets (force deduction 20)
Appium自动化测试基础 — APPium基本原理
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
168. Excel表列名称
409. 最长回文串
二叉树神级遍历:Morris遍历
Promql select time series
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
torch.histc
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
5. [WebGIS practice] software operation - service release and permission management
ECMAScript 6.0
Appium fundamentals of automated testing - basic principles of appium
Asgnet paper and code interpretation 2
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model