Springboot项目启动报错 Parameter 0 of method stringRedisTemplate in XXX

SpringBoot拦截器注入stringredistemplate出现Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.问题

问题是注入StringRedisTemplate后启动后端时报错:Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

问题出在没有配置RedisConfig类,考虑在config包中添加RedisConfig类 

public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate();
        //设置redis连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //创建Json序列化工具
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        //redis中的key序列化器
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        //redis中的Object序列化器
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return redisTemplate;
    }
}

再次运行发现还是报该错误,继续往下看了看

在springboot启动类上添加注解 @ComponentScan(basePackages = { “XXX” }),引号里面代表扫描方法,启动后还是报错

发现项目的父类pom引入的redis是

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-data-redis</artifactId>
</dependency>

将子类引入的pom改为了

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

再次启动就成功了,希望可以帮助大家。

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值