SpringBoot Redis入门——redis、Lettuce、Redisson使用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootApplication
public class RedisLettuceApplication {
public static void main(String[] args) {
SpringApplication.run(RedisLettuceApplication.class, args);
}
// 使用LettuceConnectionFactory配置RedisConnectionFactory
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
// 使用RedisTemplate<String, Object>配置RedisTemplate
@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
// 使用StringRedisTemplate配置StringRedisTemplate
@Bean
public StringRedisTemplate stringRedisTemplate() {
final StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
}
这段代码演示了如何在Spring Boot应用程序中配置Lettuce作为Redis连接库。它定义了一个LettuceConnectionFactory Bean,并且还提供了RedisTemplate和StringRedisTemplate的配置,这些模板可以用于操作Redis数据库。
评论已关闭