求求各位大佬帮助,为什么无法注入redisTemplate
依赖
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
import com.atguigu.yygh.sms.service.SmsService;
import com.atguigu.yygh.sms.utils.HttpUtils;
import com.atguigu.yygh.sms.utils.RandomUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author Cheng.Zhou
* @version 1.0
* @data 2024/12/10 17:08
*/
@Service
public class SmsServiceImpl implements SmsService {
//注入redis
@Autowired
private RedisTemplate<String,String> redisTemplate; ====>这里为null
@Override
public boolean sendCode(String phone) {
//查询redis里面是否有这个
String host = "https://gyytz.market.alicloudapi.com";
String path = "/sms/smsSend";
String method = "POST";
String appcode = "44618f6ddad242ab93a888f7b4bfd567";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
querys.put("mobile", phone);
String fourBitRandom = RandomUtil.getFourBitRandom();
querys.put("param", "**code**:" + fourBitRandom + ",**minute**:5");
//smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html
System.out.println(fourBitRandom);
querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
querys.put("templateId", "f62560824f8c40cd95dbd78ab633d244");
Map<String, String> bodys = new HashMap<String, String>();
try {
/**
* 重要提示如下:
* HttpUtils请从\r\n\t \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t \t* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
System.out.println(EntityUtils.toString(response.getEntity()));
redisTemplate.opsForValue().set(phone,fourBitRandom,5,TimeUnit.DAYS);
//获取response的body
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
new SmsServiceImpl().sendCode("1582870789");
}
}
rgs) {
new SmsServiceImpl().sendCode(“1582870789”);
}
}
[外链图片转存中...(img-O1JeenyZ-1734059379653)]
