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.core.StringRedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@SpringBootApplication
@EnableRedisRepositories
public class Redis7Application {
public static void main(String[] args) {
SpringApplication.run(Redis7Application.class, args);
}
@Bean
public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {
return new StringRedisTemplate(factory);
}
}
这段代码演示了如何在Spring Boot应用程序中集成Redis。通过@EnableRedisRepositories
注解,Spring Data Redis允许你创建Redis仓库。redisTemplate
方法提供了一个配置好的StringRedisTemplate
Bean,它使用提供的RedisConnectionFactory
来创建连接。这是一个标准的做法,适用于大多数Redis集成场景。