Shrikant Havale Shrikant Havale - 4 months ago 119x
Java Question

Spring Redis - Read configuration from file

I have Spring Redis working using

with all default configuration likes
and so on.

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

Redis Configuration File

public class SpringSessionRedisConfiguration {

JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();

RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) {
return new RedisCacheManager(stringRedisTemplate);

StringRedisTemplate template(final RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);

Standard Parameters in




What I tried,

  1. I can possibly use
    and then inject
    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
    should help me, but still cannot figure out how exactly to tell Spring to read from properties file.


You can use @PropertySource to read options from 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:

public class SpringSessionRedisConfiguration {

    private String redisHostName;

    private int redisPort;

    public static PropertySourcesPlaceholderConfigurer    propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();

    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        return factory;

    RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
        return redisTemplate;

    RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
        return redisCacheManager;

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

Please note that currently only Jedis and lettuce Lettuce support Redis Sentinel.

You may read more about this in official documentation.