StringRedisTemplate无法注入?-灵析社区

九久九

我在使用StringRedisTemplate时出现了错误,日志信息如图: ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241015/7fadfdb451266ab374e8e676a8b47eb3.png) 看样子是注入失败,可是为什么会这样呢? 我的springboot版本是2.7,redis-data版本2.7. yml文件中的配置信息如下: redis: database: 1 host: 192.168.234.130 port: 6379 timeout: 5000 password: 123456 好像什么配置都有了,版本应该也不会冲突。。 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241015/fa730248e1f2d7dfe24c51a7c8cebc67.png) 大佬们求解啊。。

阅读量:20

点赞量:0

问AI
上下文中找不到对应的 Bean。 试一下以下的几项检查: 1. 依赖: 确保你的项目依赖中包含了 Spring Data Redis 相关的库。 Maven: org.springframework.boot spring-boot-starter-data-redis 2. 配置文件: 确保你的 Spring 配置文件中有 Redis 的配置,我看你也配置了。 3. 检查是否有 Redis 相关的 Bean: 在你的 Spring 配置类或主应用程序类上,确保有 "@EnableCaching" 注解,并且有一个配置 "StringRedisTemplate" 的 Bean:import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; @Configuration public class RedisConfig { @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { return new StringRedisTemplate(redisConnectionFactory); } } 确保这个配置类被正确地扫描到了 Spring 的上下文中。 4. 检查 Redis 服务器: 确保你的 Redis 服务器在运行,并且配置的连接信息是正确的。 5. 清理和重新构建: 尝试清理构建缓存,然后重新构建项目。在 IDEA 中,通过 "Build" > "Rebuild Project" 重新构建。