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.
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.
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.
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.
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").
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 (); }
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.
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.
you can follow two ways how to use multiple RedisTemplate
s within one Spring Boot application:
@Autowired @Qualifier("beanname") RedisTemplate myTemplate
and create the bean with @Bean(name = "beanname")
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With