redisTemplate 报空指针异常
原因分析
1.是否引入正常jar包
2.调用redis工具类的时候是否@Resource自动注入
pom.xml
复制代码
1
2
3
4
5<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
application.yml
复制代码
1
2
3
4
5spring: redis: host: localhost port: 6379 password:
RedisUtil.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19@Component public class RedisUtil { @Resource RedisTemplate<String, String> redisTemplate; public String get(String key) { if (StringUtils.isEmpty(key)) { return null; } return redisTemplate.opsForValue().get(key); } public void set(String key, String value) { if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return; } redisTemplate.opsForValue().set(key, value); }
UserServiceImpl.java
复制代码
1
2
3
4@Service public class UserServiceImpl implements UserService { @Resource RedisUtil redisUtil;
复制代码
1
2
3
4
5
6
7
8@Override public String testRedis() { String key = "user"; String value = "tom"; redisUtil.set(key, value); String result = redisUtil.get(key); return result; }
最后
以上就是动听黑夜最近收集整理的关于redisTemplate 报空指针异常的全部内容,更多相关redisTemplate内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复