gpt4 book ai didi

java - Redis 问题考虑在您的配置中定义类型为 'org.springframework.data.redis.core.HashOperations' 的 bean

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

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

}

我的 repo 类:

@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();
}

我的 Controller 类如下,

@RestController
@RequestMapping("/rest/user")
public class RedisController {
private UserRepository userRepository;

public RedisController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@GetMapping("/add/{id}/{name}")
public User add(@PathVariable String id,@PathVariable String name){
userRepository.save(new User(name,id,25000));
return userRepository.findById(id);
}
}

当我运行应用程序时出现以下错误,

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisController' defined in file [redisDemo\RedisController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepoImpl' defined in file [redisDemo\repository\UserRepoImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.HashOperations' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

程序有什么问题?有什么建议让它发挥作用吗?我需要使用 jedisconnectionFactory 吗?我正在使用 Intellij,我在 Windows 上的 redis 版本是 3.2spring 是 2.1.6

最佳答案

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

public UserRepoImpl(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
}

关于java - Redis 问题考虑在您的配置中定义类型为 'org.springframework.data.redis.core.HashOperations' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56746895/

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