运行项目的时候出现了redisTemplate注入失败的问题
配置redis(来自两个人的配置,作以学习)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000
spring:
redis:
database: 1
host: localhost
port: 6379
password:
lettuce:
pool:
max-active: 200
max-wait: 10
min-idle: 0
timeout: 1000
在 Spring Boot 1.x 版本默认使用的是 jedis ,而在 Spring Boot 2.x 版本默认使用的就是Lettuce。所以这里会更加偏向于使用lettuce。
总结
总结一下几种解决方案:
1.将RedisTemplate对象上的注解从@Autowired改到@Resource
2.在注入RedisTemplate的时候,将后面的带的“<>”里的参数去掉
以上的两种方案来自于
https://blog.csdn.net/zhaoheng314/article/details/81564166
我没有试过,不做评价
3.在yml或在properties文件中,把lettuce配置redis的相关代码改到jedis下(简单来说就是把lettuce这个单词改成jedis),或者直接去掉lettuce相关的配置,使用lettuce自动配置,一般情况下也够用了
4.添加依赖:
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
在创建Springboot项目的时候,我们会添加spring-boot-starter-data-redis这个依赖,很可能会忽略上面这个,所以很多教程也会忽略掉,不仔细看就会报错。