gpt4 book ai didi

java.lang.ClassCastException : com. sun.proxy.$Proxy219 无法转换为 org.springframework.data.redis.connection.StringRedisConnection

转载 作者:可可西里 更新时间:2023-11-01 11:24:34 28 4
gpt4 key购买 nike

这是redis的一个函数,pipeline。以前用过,今天测试这个功能的时候。它不起作用。

public List<Object> hgetpipeline(List<String> keys) {
List<Object> results = redisTemplate.executePipelined(
new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
for(int i=0; i< keys.size(); i++) {
stringRedisConn.hGetAll(keys.get(i)+"");
}
return null;
}
});
return results;
}

这一行发生了一些错误

StringRedisConnection stringRedisConn = (StringRedisConnection)connection;

有错误信息

java.lang.ClassCastException: 
com.sun.proxy.$Proxy219 cannot be cast to org.springframework.data.redis.connection.StringRedisConnection

主类中的注释

@SpringBootApplication
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@EnableScheduling
@RestController
public class ApiApplication {

public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}

@RequestMapping("/")
public String index() {
return "Server started!";
}

}

最佳答案

您必须使用 stringRedisTemplate。例如:

@Resource
private StringRedisTemplate stringRedisTemplate;

public List<Object> hgetpipeline(List<String> keys) {
List<Object> results = stringRedisTemplate.executePipelined(
new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
for(String key:keys)
{
stringRedisConn.get(key);
}
return null;
}
});
return results;
}

关于java.lang.ClassCastException : com. sun.proxy.$Proxy219 无法转换为 org.springframework.data.redis.connection.StringRedisConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50680948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com