SpringBoot整合redis报错

在SpringBoot中整合Redis进行Token登录,测试方法中一直报错:

1
2
3
java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'redisTemplate' is expected to be of type 'org.springframework.data.redis.core.StringRedisTemplate' but was actually of type 'org.springframework.data.redis.core.RedisTemplate'

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public boolean set(String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

// 测试方法
@Test
public void setTest() {
redisClient.set("key1", "value1");
String value = redisClient.get("key1");
assertNotNull(value);
log.info("value: {}", value);
}

原来是注入的时候变量名有问题:

1
2
@Resource
private StringRedisTemplate redisTemplate;

不是 redisTemplate ,而是 stringRedisTemplate。

@Resource和@Autowired的区别。

文章作者: iced-cola
文章链接: https://iced-cola.com/2021/03/07/SpringBoot整合redis报错/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 iced-cola