Java 如何在Spring Boot中配置Redis缓存?

Java 如何在Spring Boot中配置Redis缓存?,java,spring,caching,spring-boot,redis,Java,Spring,Caching,Spring Boot,Redis,如何使用Spring Boot配置Redis缓存。据我所知,这只是application.properties文件中的一些更改,但不知道具体是什么 您可以在application.properties文件中提及所有必需的属性,即主机名、端口等,然后从中读取 @Configuration @PropertySource("application.properties") public class SpringSessionRedisConfiguration { @Value("${redis.

如何使用Spring Boot配置Redis缓存。据我所知,这只是
application.properties
文件中的一些更改,但不知道具体是什么

您可以在application.properties文件中提及所有必需的属性,即主机名、端口等,然后从中读取

@Configuration
@PropertySource("application.properties")
public class SpringSessionRedisConfiguration {

@Value("${redis.hostname}")
private String redisHostName;

@Value("${redis.port}")
private int redisPort;

@Bean
public static PropertySourcesPlaceholderConfigurer    propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(redisHostName);
    factory.setPort(redisPort);
    factory.setUsePool(true);
    return factory;
}

@Bean
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

@Bean
RedisCacheManager cacheManager() {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
    return redisCacheManager;
}
@配置
@PropertySource(“application.properties”)
公共类SpringSessionRedisConfiguration{
@值(${redis.hostname}”)
私有字符串主机名;
@值(${redis.port}”)
私人int再报告;
@豆子
公共静态属性资源占位符配置器属性资源占位符配置器(){
返回新属性资源占位符配置器();
}
@豆子
绝地连接工厂绝地连接工厂(){
JedisConnectionFactory=新的JedisConnectionFactory();
setHostName(redisHostName);
工厂设置端口(重新端口);
factory.setUsePool(true);
返回工厂;
}
@豆子
RedisTemplate RedisTemplate(){
RedisTemplate RedisTemplate=新RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
返回模板;
}
@豆子
RedisCacheManager缓存管理器(){
RedisCacheManager RedisCacheManager=新的RedisCacheManager(redisTemplate());
返回管理器;
}

}

要在Spring boot应用程序中使用
Redis
缓存,只需在
应用程序.properties文件中设置这些属性即可

spring.cache.type=redis
spring.redis.host=localhost //add host name here
spring.redis.port=6379
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

我想,这不是弹簧靴的方式。在Spring Boot中,我们不需要创建所有这些缓存管理器和东西(根据我所读到的和听到的),我想这会给您在配置上提供更多的灵活性。这是真的,但我需要Spring Boot方式您找到Spring Boot方式了吗?
@Autowired
RedisCacheManager redisCacheManager;