springboot连接redis并动态切换database
众所周知,redis多有个db,在jedis中可以使用select方法去动态的选择redis的database,但在springboot提供的StringRedisTemplate中确,没有该方法,好在StringRedisTemplate预留了一个setConnectionFactory方法,本文主为通过修改ConnectionFactory从而达到动态切换database的效果。
springboot连接redis
pom.xml文件中引入spring-boot-starter-redis,版本可自行选择
org.springframework.boot
spring-boot-starter-redis
1.3.8.RELEASE
application.properties
#redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=pwd
spring.redis.timeout=0
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
TestCRedis.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTes