Springboot encountered an issue calling the Reids template object RedisTemplate that could not automatically inject custom generics. Only RedisTemplate
and RedisTemplate

can be injected automatically.
,>
,>

1. View the default Springboot configuration

@Configuration( proxyBeanMethods = false )
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
    public RedisAutoConfiguration(a) {}@Bean
    @ConditionalOnMissingBean( name = {"redisTemplate"} )
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        returntemplate; }}Copy the code

As you can see, SpringBoot defines two types of bean for us by default: RedisTemplate
, StringRedisTemplate. @autoWired can only inject like this, no other generic injection can be used.,>

  • Because @autoWired is injected by type

  • Workaround: @Resource injection can be used, and custom objects need to implement the Serializable interface because Spring serializes objects before storing them in Redis.