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

我正在尝试通过 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);

    }


}

我的回购课程:


@Repository

public 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();

}


侃侃尔雅
浏览 93回答 1
1回答

万千封印

您不需要HashOperations hashOperations在构造函数中作为参数传递UserRepoImpl。相反,您可以从构造函数中删除此参数。public UserRepoImpl(RedisTemplate redisTemplate) {&nbsp; &nbsp; this.redisTemplate = redisTemplate;&nbsp; &nbsp; this.hashOperations = redisTemplate.opsForHash();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java