本文介绍了Spring Redis - 从application.properties文件读取配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Spring Redis使用 spring-data-redis 使用所有默认配置 localhost $ c> port 等等。

I have Spring Redis working using spring-data-redis with all default configuration likes localhost default port and so on.

现在我正试图通过在 .properties 文件。但是我不知道我应该如何创建正确的属性值被读取的bean。

Now I am trying to make the same configuration by configuring it in application.properties file. But I cannot figure out how should I create beans exactly that my property values are read.

Redis配置文件

@EnableRedisHttpSession
@Configuration
public class SpringSessionRedisConfiguration {

@Bean
JedisConnectionFactory connectionFactory() {
    return new JedisConnectionFactory();
}

@Autowired
@Bean
RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) {
    return new RedisCacheManager(stringRedisTemplate);
}

@Autowired
@Bean
StringRedisTemplate template(final RedisConnectionFactory connectionFactory) {
    return new StringRedisTemplate(connectionFactory);
}
}

application.properties中的标准参数

Standard Parameters in application.properties

spring.redis.sentinel.nodes = 192.168 .188.231:26379

spring.redis.sentinel.nodes=192.168.188.231:26379

spring.redis.password = 12345

spring.redis.password=12345

我试过,


  1. 我可以使用 @PropertySource $ c> @Value 并获取值。但我不想这样做,因为这些属性不是由我定义的,而是来自Spring。

  2. 在本文档,它只说它可以配置使用

  3. 我还浏览了Spring Data Redis API类,发现 RedisProperties 应该可以帮助我,但仍然无法弄清楚如何告诉Spring如何从属性文件中读取。

  1. I can possibly use @PropertySource and then inject @Value and get the values. But I don't want to do that as those properties are not defined by me but are from Spring.
  2. In this documentation Spring Redis Documentation, it only says that it can be configured using properties but doesn't show concrete example.
  3. I also went through Spring Data Redis API classes, and found that RedisProperties should help me, but still cannot figure out how exactly to tell Spring to read from properties file.


推荐答案

您可以使用 @PropertySource 从application.properties或其他所需的属性文件中读取选项。请查看,并使用。或查看以下小范例:

You can use @PropertySource to read options from application.properties or other property file you want. Please look PropertySource usage example and working example of usage spring-redis-cache. Or look at this small sample:

@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;
    }
}

strong>) application.properties 中的 spring.redis.sentinel 选项支持 RedisSentinelConfiguration

In present time (december 2015) the spring.redis.sentinel options in application.properties has limited support of RedisSentinelConfiguration:

您可以在。

这篇关于Spring Redis - 从application.properties文件读取配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:26