springboot配置jedispool的方法是什么
在Spring Boot中配置Jedis Pool时,通常首先要引入Jedis依赖。
- 添加Jedis依赖:
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>
- 在application.properties或application.yml文件中配置Redis连接信息:
spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=spring.redis.jedis.pool.max-active=8spring.redis.jedis.pool.max-idle=8spring.redis.jedis.pool.min-idle=0spring.redis.jedis.pool.max-wait=-1
- 创建Jedis配置类:
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class JedisConfig {@Beanpublic JedisConnectionFactory jedisConnectionFactory() {JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();jedisConnectionFactory.setHostName("127.0.0.1");jedisConnectionFactory.setPort(6379);return jedisConnectionFactory;}@Beanpublic RedisTemplate<String, String> redisTemplate() {RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(jedisConnectionFactory());redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());return redisTemplate;}}
这样就可以配置并使用Jedis Pool来连接Redis数据库了。在其他类中可以通过@Autowired注解来注入RedisTemplate,然后使用其操作Redis数据。