美文网首页
Springboot集成Redis找不到Bean

Springboot集成Redis找不到Bean

作者: 鹅鹅鹅_ | 来源:发表于2019-06-15 00:41 被阅读0次

最近打算在自己的小项目中引入redis。

引入依赖

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

编写配置,我使用的是lettuce

@Configuration
public class RedisConfig {

    @Primary
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

然后,启动测试报错了

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stringRedisTemplate' defined in class path resource

或者

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource

最后发现是因为lettuce,依赖commons-pool2,所以引入该包即可。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

相关文章

网友评论

      本文标题:Springboot集成Redis找不到Bean

      本文链接:https://www.haomeiwen.com/subject/qhkvfctx.html