在SpringBoot中配置JedisPool时,通常首先要引入Jedis依赖
在SpringBoot中配置JedisPool时,通常首先要引入Jedis依赖。
- 添加Jedis依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-wait=-1
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;
importorg.springframework.data.redis.core.RedisTemplate;
importorg.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
publicclassJedisConfig{
@Bean
publicJedisConnectionFactoryjedisConnectionFactory(){
JedisConnectionFactoryjedisConnectionFactory=newJedisConnectionFactory();
jedisConnectionFactory.setHostName("127.0.0.1");
jedisConnectionFactory.setPort(6379);
returnjedisConnectionFactory;
}
@Bean
publicRedisTemplate<String,String>redisTemplate(){
RedisTemplate<String,String>redisTemplate=newRedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(newStringRedisSerializer());
redisTemplate.setValueSerializer(newStringRedisSerializer());
returnredisTemplate;
}
}
这样就可以配置并使用JedisPool来连接Redis数据库了。在其他类中可以通过@Autowired注解来注入RedisTemplate,然后使用其操作Redis数据。
版权声明
本文仅代表作者观点,不代表博信信息网立场。