Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 375
Derek Avatar asked Feb 20 '16 00:02

Derek


People also ask

What is Redis in spring boot?

Redis is driven by a keystore-based data structure to persist data and can be used as a database, cache, message broker, etc. We'll be able to use the common patterns of Spring Data (templates, etc.) while also having the traditional simplicity of all Spring Data projects.

What is spring boot starter data Redis?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.

What is RedisTemplate in Java?

public class RedisTemplate<K,V> extends RedisAccessor implements RedisOperations<K,V>, BeanClassLoaderAware. Helper class that simplifies Redis data access code. Performs automatic serialization/deserialization between the given objects and the underlying binary data in the Redis store.

How to use multiple redistemplates within one Spring Boot application?

1 Answer 1. you can follow two ways how to use multiple RedisTemplates within one Spring Boot application: Named bean injection with @Autowired @Qualifier("beanname") RedisTemplate myTemplate and create the bean with @Bean(name = "beanname").

How do I create a resttemplate in Spring Boot?

Configuration Using the Default RestTemplateBuilder To configure a RestTemplate this way, we need to inject the default RestTemplateBuilder bean provided by Spring Boot into our classes: private RestTemplate restTemplate; @Autowired public HelloController(RestTemplateBuilder builder) { this .restTemplate = builder.build (); }

How to create and consume a Spring Boot RESTful service?

Consuming and creating a Spring Boot RESTful web service requires a lot of boiler-plate code. The Spring Boot RestTemplate makes it easy to create and consume RESTful web service. RestTemplate is a Spring REST client which we can use to consume different REST APIs.

Is it possible to manually call redistemplate's afterpropertiesset?

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.


1 Answers

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());

        return stringTemplate;
    }

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

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

        return byteTemplate;
    }

}

HTH, Mark

like image 148
mp911de Avatar answered Dec 28 '22 08:12

mp911de