redisConnectionFactory标红Could not autowire. No beans of 'RedisConnectionFactory' type found.
时间: 2023-11-17 15:53:07 浏览: 168
问题描述中提到了在创建redisTemplate时出现了错误,错误信息是"Could not autowire. No beans of 'RedisConnectionFactory' type found." \[1\] 这个错误通常是由于Spring Boot版本的问题引起的。根据资料,大部分2.x版本的Spring Boot都会出现这个问题,但并不影响正常使用\[2\]。
解决这个问题的方法是在配置类中手动创建RedisConnectionFactory的bean,并将其注入到redisTemplate中。可以参考下面的代码示例:
```java
@Bean
public RedisConnectionFactory redisConnectionFactory() {
// 在这里配置Redis连接工厂的相关信息
// 例如使用Lettuce连接池:
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
return factory;
}
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
```
通过手动创建RedisConnectionFactory的bean,并将其注入到redisTemplate中,可以解决"Could not autowire. No beans of 'RedisConnectionFactory' type found."的问题\[3\]。
#### 引用[.reference_title]
- *1* [Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.已解决](https://blog.csdn.net/baiqi123456/article/details/128318413)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Spring Boot 整合redis:Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.](https://blog.csdn.net/qq_32923975/article/details/130484863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.](https://blog.csdn.net/qq_67972273/article/details/126388082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文