Parameter 0 of method redisTemplate in com.chair.chairdada.config.RedisConfig required a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' that could not be found.

这个错误表明在RedisConfig类中,RedisTemplate的构造函数需要一个RedisConnectionFactory类型的bean,但是Spring容器中找不到这个bean。

要解决这个问题,你需要在Spring Boot应用程序中添加一个RedisConnectionFactory的bean。你可以使用LettuceConnectionFactoryJedisConnectionFactory,取决于你选择的Redis客户端。

以下是如何添加LettuceConnectionFactory的示例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }
	// 或者下边的代码,效果一样
//    @Bean
//    public JedisConnectionFactory redisConnectionFactory() {
//        return new JedisConnectionFactory();
//    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        return template;
    }
}

这样,Spring容器就会自动创建LettuceConnectionFactory的bean,并将其注入到RedisTemplate的构造函数中。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐