为了账号安全,请及时绑定邮箱和手机立即绑定

Redis 问题考虑在您的配置中定义类型为“org.springframework.data.

Redis 问题考虑在您的配置中定义类型为“org.springframework.data.

侃侃尔雅 2023-02-16 15:37:08
我正在尝试通过 java 连接到 Redis,我有以下配置,@SpringBootApplication@Configuration@ComponentScan(basePackages = "com.infy.redisDemo")public class RedisDemoApplication {    @Bean    public LettuceConnectionFactory getConnectionFactory(){        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory("localhost",6379);        return lettuceConnectionFactory;    }    @Bean    public RedisTemplate<String,Object> getRedisTemplate(){        RedisTemplate<String, Object> redisTemplate = new RedisTemplate();        redisTemplate.setConnectionFactory(getConnectionFactory());        return redisTemplate;    }    public static void main(String[] args) {        SpringApplication.run(RedisDemoApplication.class, args);    }}我的回购课程:@Repositorypublic class UserRepoImpl implements UserRepository {    private RedisTemplate<String, Object> redisTemplate;    private HashOperations hashOperations;    public final String key = "USER";    public UserRepoImpl(RedisTemplate redisTemplate,      HashOperations hashOperations) {        this.redisTemplate = redisTemplate;        this.hashOperations = redisTemplate.opsForHash();    }    @Override    public void save(User user) {        hashOperations.put(key,user.getId(),user);    }    @Override    public void update(User user) {        hashOperations.put(key,user.getId(),user);    }    @Override    public Map findAll() {        return hashOperations.entries(key);    }}public interface UserRepository {    void save(User user);    void update(User user);    Map findAll();}
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

您不需要HashOperations hashOperations在构造函数中作为参数传递UserRepoImpl。相反,您可以从构造函数中删除此参数。


public UserRepoImpl(RedisTemplate redisTemplate) {

    this.redisTemplate = redisTemplate;

    this.hashOperations = redisTemplate.opsForHash();

}


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信