在SpringBoot中整合Redis进行Token登录,测试方法中一直报错:
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'
代码:
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);
}
原来是注入的时候变量名有问题:
@Resource
private StringRedisTemplate redisTemplate;
不是 redisTemplate ,而是 stringRedisTemplate。
@Resource和@Autowired的区别。