在springboot项目中添加了redis的配置类

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    @Resource
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //默认的Key序列化器为:JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }

}
redis:
    host: localhost
    port: 6379
#    password: 123456
    database: 0 #操作的是0号数据库
    jedis:
      #Redis连接池配置
      pool:
        max-active: 8 #最大连接数
        max-wait: 1ms #连接池最大阻塞等待时间
        max-idle: 4 #连接池中的最大空闲连接
        min-idle: 0 #连接池中的最小空闲连接

 配置文件也没有什么问题,但是就是一执行@autowried就会报Error creating bean with name 'redisTemplate'

按理来说应该是正常的

于是更换了一下maven坐标的版本结果解决了,应该就是版本与Springboot版本进行冲突导致的.更改为如下版本发现问题可以解决,redisTemplate对象可以正常注入了.

还有一点大家在进行redis的学习中,在springboot测试目录进行编写的话一定记得在测试类加上@RunWith(SpringRunner.class)注解,如果测试过程没加也会报些奇奇怪怪的错误!

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐