问题: 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>