A component required a bean named 'redisTemplate' that could not be found.
자유 게시판 프로젝트를 진행하던 중 나는 조회수와 댓글의 좋아요를 관리하기 위해 NoSQL 데이터베이스인 Redis를 사용했다. 사용하던 와중에 Redis의 데이터베이스를 관리하던 도중 위와 같은 에러가 발생했다.
왜 redisTemplate 오류가 났었는가?
- 해당 에러는 Spring이 redisTemplate라는 이름의 bean을 찾지 못했기 때문에 나타나는 에러이다.
- 혹시 해당 프로젝트에서 redis의 데이터베이스를 나눠서 사용하는 프로젝트라면 해당 문제가 발생할 수도 있다.
문제 상황
이번 프로젝트에서 조회수를 관리하는 viewCount와 댓글의 좋아요를 commentLike가 존재하는데, viewCount는 redis의 0번째 데이터베이스, commentLike는 1번째 데이터베이스에서 관리하도록 설정을 했다.
[문제가 발생한 코드]
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory viewCountRedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setDatabase(0);
return new LettuceConnectionFactory(configuration);
}
@Bean
public StringRedisTemplate viewCountRedisTemplate() {
return new StringRedisTemplate(viewCountRedisConnectionFactory());
}
@Bean
public RedisConnectionFactory commentLikeRedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setDatabase(1);
return new LettuceConnectionFactory(configuration);
}
@Bean
public StringRedisTemplate commentLikeRedisTemplate() {
return new StringRedisTemplate(commentLikeRedisConnectionFactory());
}
}
[결과]
A component required a bean named 'redisTemplate' that could not be found.
문제 원인
이는 SpringBoot에서 spring-boot-data-redis 라이브러리 의존성을 주입받으면 Spring은 기본적으로 RedisTemplate<String, String> Bean을 제공하려고 시도하고, 찾게 되는데 이때 기본적으로 redisTemplate라는 이름으로 등록된다.
하지만 현재 RedisConfig 설정에서 수동으로 Redis를 제어하고 있기 때문에 SpringBoot의 자동설정에서(RedisAutoConfiguration)이 동작하지 않는다.
RedisTemplate를 제어하려고 하니 redisTemplate라는 이름의 Bean을 명시적으로 등록하지 않았으므로 해당 문제가 발생하게 되는것이다. 이와 같은 이유로 해당 에러가 발생한 것이다.
위에 문제의 코드에서 StringRedisTemplate가 있지만 redisTemplate는 아니다.
StringRedisTemplate는 RedisTemplate<String,String>을 상속받지만 Spring은 기본적으로 이름이 “redisTemplate”인 RedisTemplate Bean을 찾고있기 때문에 위에 에러가 나타난다.
문제원인 정리
- spring-boot-data-redis를 사용하면 스프링 부트가 자동으로 RedisTemplate를 제공하고, 런타임시 해당 클래스를 주입하려고 시도한다.
- 하지만 해당 프로젝트에서 RedisConfig를 통해 직접 수동으로 제어하다보니 스프링부트의 자동설정작업이 동작하지 않는다.
- 위와 같은 이유로 redisTemplate를 찾을 수 없다고 나오는 것.
해결방법
기본 RedisTemplate (Spring이 찾을 수 있도록 제공)
@Bean
public RedisConnectionFactory redisConnectionFactoryForSpring() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
return new LettuceConnectionFactory(configuration);
}
@Bean
public RedisTemplate<String, Object> **redisTemplate**(@Qualifier("redisConnectionFactoryForSpring") RedisConnectionFactory redisConnectionFactoryForSpring) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactoryForSpring);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
return template;
}
기본적으로 스프링부트가 찾을 수 있는 RedisTemplate<String, String> 를 redisTemplate라는 이름으로 등록한다.
이렇게 하면 런타임시 스프링부트는 해당 빈을 찾아 주입을 하고, 실제 프로덕션 코드에서는 우리가 실제로 사용할 Redis를 레포지토리 레이어에서 의존성을 주입받아 사용할 수 있다.
// 📌 View Count 전용 StringRedisTemplate
@Bean
public RedisConnectionFactory viewCountRedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setDatabase(0);
return new LettuceConnectionFactory(configuration);
}
@Bean
public StringRedisTemplate viewCountRedisTemplate(@Qualifier("viewCountRedisConnectionFactory") RedisConnectionFactory viewCountRedisConnectionFactory) {
return new StringRedisTemplate(viewCountRedisConnectionFactory);
}
// 📌 Comment Like 전용 StringRedisTemplate
@Bean
public RedisConnectionFactory commentLikeRedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setDatabase(1);
return new LettuceConnectionFactory(configuration);
}
@Bean
public StringRedisTemplate commentLikeRedisTemplate(@Qualifier("commentLikeRedisConnectionFactory") RedisConnectionFactory commentLikeRedisConnectionFactory) {
return new StringRedisTemplate(commentLikeRedisConnectionFactory);
}
RedisConnectionFactory를 직접 주입하면?
- Spring이 기본적으로 관리하는 RedisConnectionFactory 가 자동으로 주입받는다.
요약
- Spring Boot는 기본적으로 RedisTemplate<String, String>을 redisTemplate이라는 이름으로 자동 등록하려고 한다.
- 하지만, 수동으로 RedisConfig를 작성하는 순간 자동 설정이 무효화된다.
- 그런데 RedisTemplate을 만들긴 했지만, Spring이 기대하는 redisTemplate이라는 이름으로 등록하지 않았다.
- 그래서 Spring이 redisTemplate을 찾을 수 없어서 오류가 발생한 것!
- redisTemplate이라는 이름을 직접 등록하면 Spring이 이를 찾을 수 있게 된다.
'프로젝트 이슈 및 몰랐던점 정리 > CommunityAPI' 카테고리의 다른 글
[학습 포인트]💡 MockMvc 컨트롤러 단위 테스트 시 인증정보 부재 (0) | 2025.03.30 |
---|---|
[학습 포인트] 💡Redis 단위 테스트 작성하기 feat.ValueOperations, Operations (0) | 2025.03.30 |
[트러블 슈팅] ⚠️ 읽기 전용 트랜잭션 내 쓰기 작업 문제와 REQUIRES_NEW 전파 전략 활용 (0) | 2025.03.30 |
[트러블 슈팅] ⚠️ Spring Boot LocalDateTime 변환 에러 해결법 (@JsonFormat vs @DateTimeFormat) (0) | 2025.03.09 |
[트러블 슈팅] ⚠️ @RequestBody로 MultipartFile을 받을 수 없는 이유와 해결법(@RequestPart) (0) | 2025.03.09 |