How to create a second RedisTemplate instance in a Spring Boot application

Derek picture Derek · Feb 20, 2016 · Viewed 7.2k times · Source

According to this answer, one RedisTemplate cannot support multiple serializers for values. So I want to create multiple RedisTemplates for different needs, specifically one for string actions and one for object to JSON serializations, to be used in RedisCacheManager. I'm using Spring Boot and the current RedisTemplate is autowired, I'm wondering what's the correct way to declare a second RedisTemplate instance sharing the same Jedis connection factory but has its own serializers?

Tried something like this in two different components,

Component 1 declares,

    @Autowired
    private RedisTemplate redisTemplate;

    redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Instance.class));

Component 2 declares,

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

In this case the two templates actually are the same. Traced into Spring code and found component 1's template got resolved to autoconfigured stringRedisTemplate.

Manually calling RedisTemplate's contructor and then its afterPropertiesSet() won't work either as it complains no connection factory can be found.

I know this request probably is no big difference from defining another bean in a Spring app but not sure with the current Spring-Data-Redis integration what's the best way for me to do. Please help, thanks.

Answer

mp911de picture mp911de · Feb 21, 2016

you can follow two ways how to use multiple RedisTemplates within one Spring Boot application:

  1. Named bean injection with @Autowired @Qualifier("beanname") RedisTemplate myTemplate and create the bean with @Bean(name = "beanname").
  2. Type-safe injection by specifying type parameters on RedisTemplate (e.g. @Autowired RedisTemplate<byte[], byte[]> byteTemplate and @Autowired RedisTemplate<String, String> stringTemplate).

Here's the code to create two different:

@Configuration
public class Config {

    @Bean
    public RedisTemplate<String, String> stringTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, String> stringTemplate = new RedisTemplate<>();
        stringTemplate.setConnectionFactory(redisConnectionFactory);
        stringTemplate.setDefaultSerializer(new StringRedisSerializer());
        stringTemplate.afterPropertiesSet();

        return stringTemplate;
    }

    @Bean
    public RedisTemplate<byte[], byte[]> byteTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<byte[], byte[]> byteTemplate = new RedisTemplate<>();
        byteTemplate.setConnectionFactory(redisConnectionFactory);
        byteTemplate.afterPropertiesSet();

        return byteTemplate;
    }

}

HTH, Mark