问题: springboot2 配置RedisTemplate 后,启动报错创建 stringRedisTemplate/redisTemplate异常

报错内容

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of
 resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'stringRedisTemplate' defined in class path resource 
[org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied dependency expressed 
through method 'stringRedisTemplate' parameter 0; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method 'redisConnectionFactory' 
threw exception; nested exception is java.lang.NoClassDefFoundError: 
org/apache/commons/pool2/impl/GenericObjectPoolConfig

读报错知,重点报错为
Factory method ‘redisConnectionFactory’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

原因

**spring-boot-starter-data-redis 默认采用 lettuce作为redis客户端, lettuce 底层采用 netty实现	, 可以在多个线程中并发访问,且线程安全,在使用lettuce需要配置线程池**

解决方式

在maven中引入 线程池,如下依赖
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.9.0</version>
    </dependency>

引用自该博客

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐