6

I've read that you can have @Autowired generics as of Spring 4, which is awesome.

I have an abstract RedisService class in which I want to have @Autowired a generic RestTemplate, like so:

public abstract class RedisService<T> implements InitializingBean {

private final String VALUE_KEY_PREFIX;
private final String SET_KEY;

@Autowired
private RedisTemplate<String, T> valueTemplate;

@Autowired
private StringRedisTemplate stringTemplate;

private SetOperations<String, String> setOperations;
private ValueOperations<String, T> valueOperations; 
// and so on...
}

My Java config for the valueTemplates to be @Autowired then looks something like:

@Bean
public RedisTemplate<String, MyTypeA> myTypeARedisTemplate() {
    RedisTemplate<String, MyTypeA> template = new RedisTemplate<>();

    template.setKeySerializer(stringRedisSerializer());
    template.setHashKeySerializer(stringRedisSerializer());
    template.setValueSerializer(new Jackson2JsonRedisSerializer<>(MyTypeA.class));
    template.setConnectionFactory(jedisConnectionFactory());

    return template;
}

@Bean
public RedisTemplate<String, MyTypeB> myTypeBRedisTemplate() {
    RedisTemplate<String, MyTypeB> template = new RedisTemplate<>();

    template.setKeySerializer(stringRedisSerializer());
    template.setHashKeySerializer(stringRedisSerializer());
    template.setValueSerializer(new Jackson2JsonRedisSerializer<>(MyTypeB.class));
    template.setConnectionFactory(jedisConnectionFactory());

    return template;
}
// ... for N MyType classes.

Each class which extends the RedisService<T> class looks something like:

@Service
public class MyTypeAService extends RedisService<MyTypeA> { 

Is there a more DRY way I could be creating these RedisTemplate @Beans with my Java config?

Tony Ghita
  • 275
  • 1
  • 4
  • 13

1 Answers1

-1

I had the same issue. You could create a generic bean for this, and use the GenricJackson2JsonRedisSerializer. The issue is that by using this the json will be saved with some extra data for the deserialization to work later on.

Another way would be to use the Jackson2JsonRedisSerializer but it needs the Class instance of the generic type to work, witch i couldnt figure out how to get.

@Bean("objectRedisTemplate")
public <T> RedisTemplate<String, T> objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();

    redisTemplate.setConnectionFactory(redisConnectionFactory);

    RedisSerializer<String> serializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(serializer);
    redisTemplate.setHashKeySerializer(serializer);

    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

    return redisTemplate;
}

Usage on cache class:

@Autowired
@Qualifier("objectRedisTemplate")
private RedisTemplate<String, MyTypeA> redisTemplate;
  • This does not seem to provide an answer to the question. You can [search for similar questions](https://stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](https://stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](https://stackoverflow.com/tour) – ascripter Apr 05 '18 at 16:04
  • @BahramdunAdil can you please elaborate? It worked fine when i posted it. – Cesar Angiolucci Oct 07 '19 at 21:57