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.
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: 5000Inject 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
@Resourceresolves 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; @Resourcematches 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.
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'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
