Why Does Spring Boot Return Null from Redis? Understanding Template Mismatches

This article explains how Spring Boot integrates Redis, reveals a common pitfall where different RedisTemplate beans cause null reads despite existing data, and shows how to diagnose and fix the issue by aligning bean types and injection annotations.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Why Does Spring Boot Return Null from Redis? Understanding Template Mismatches

Basic Spring Boot Redis Integration

Add the starter dependency to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configure connection properties in application.yml (or application.properties), for example:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    database: 1
    password: 123456
    timeout: 5000

Inject RedisTemplate (or StringRedisTemplate) and use it in a test class:

@SpringBootTest
@RunWith(SpringRunner.class)
public class TokenTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void getValue() {
        Object value = redisTemplate.opsForValue().get("1");
        System.out.println("value:" + value);
    }
}

Root Cause of Null Reads

Spring Boot’s RedisAutoConfiguration defines two beans:

@Configuration
public class RedisAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(name = {"redisTemplate"})
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory cf) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(cf);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory cf) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(cf);
        return template;
    }
}

The first bean ( redisTemplate) works with Object keys/values; the second ( stringRedisTemplate) works with String keys/values. If data is stored with one template and read with the other, the differing serializers cause the read to return null.

Impact of @Resource vs @Autowired

@Resource

resolves by bean name first, then by type. @Autowired resolves solely by type.

In the example, the storing component uses:

@Autowired
private RedisTemplate<String, String> redisTemplate;

while the retrieving component uses:

@Resource
private RedisTemplate<String, String> redisTemplate;
@Resource

matches the bean named redisTemplate, which is the RedisTemplate<Object, Object> instance. @Autowired matches the StringRedisTemplate bean. The mismatch leads to null values.

Resolution Strategies

Replace @Resource with @Autowired so both injections are type‑based.

Specify the bean name in @Resource as stringRedisTemplate to explicitly request the correct bean.

Standardize on a single template throughout the project—either always use RedisTemplate<Object, Object> or always use StringRedisTemplate.

Key Takeaway

Spring Boot’s auto‑configuration simplifies Redis usage, but the presence of two default RedisTemplate beans and the differing injection semantics of @Resource and @Autowired can cause hard‑to‑detect null reads. Consistent injection practices and awareness of which template is used prevent this issue.

JavaRedisSpringBootDependencyInjectionRedisTemplate
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.