문제 상황
프로젝트를 진행하는 도중 다음과 같은 오류가 발생했다.
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
APPLICATION FAILED TO START
Description:
The bean 'mongoMessageRepository', defined in com.dancing_orangutan.ukkikki.chat.infrastructure.MongoMessageRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in com.dancing_orangutan.ukkikki.chat.infrastructure.MongoMessageRepository defined in @EnableJpaRepositories declared on JpaConfig and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
원인 분석
오류 메시지를 분석해보니 mongoMessageRepository가 빈으로 두번 정의되어 에러가 발생하는걸 볼 수 있다.
- @EnableMongoRepositories 어노테이션이 먼저 MongoDB 관련 레포를 스캔하여 MongoMessageRepository를 등록
- @EnableJpaRepositories가 동일한 이름의 MongoMessageRepository를 JPA환경에서 등록하려고함
- Boot에서 동일한 빈을 두 번 등록할 수 없기에 Boot 실행불가
해결방법
해결방법을 찾아보니 2개의 문제 해결방법이 존재한다.
1. spring.main.allow-bean-definition-overriding=true 설정 (비추천)
.yml파일에 이 설정을 추가하면 Spring이 동일한 빈을 덮어 쓰도록 허용이 가능하다.
하지만 이 방법은 예상치 못한 빈 충돌을 허용한다는 말이기에 최선의 방법이 아니다.
2.JPA와 MongoDB의 Repository스캔 범위를 분리 (권장)
이 방법이 가장 추천되는 방법이다. 문제원인이 스캔을 하는 도중에 두번 빈을 등록해서 원인이 발생하는거 이기에 스캔 범위를 설정해서 두번 설정하는걸 회피하면된다.
@Configuration
@EnableJpaAuditing
@EnableJpaRepositories(
basePackages = "com.dancing_orangutan.ukkikki",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
classes = { RefreshTokenRepository.class, MongoMessageRepository.class }
)
)
public class JpaConfig {
}
@Configuration
@EnableMongoRepositories(
basePackages = "com.dancing_orangutan.ukkikki.chat.infrastructure"
)
public class MongoConfig {
}
'Spring' 카테고리의 다른 글
[KAFKA] Spring Boot로 KAFKA 사용해보기 (0) | 2025.04.02 |
---|---|
[해결 방안] Spring STOMP content-length 초과 에러 해결하기 (0) | 2025.03.31 |
[해결 방안] Spring Gateway를 통해 Stomp를 설정했을때 헤더가 두개오는 문제 해결방안 (1) | 2025.03.31 |
[개선사항] Spring Events을 활용하여 결합도 낮추기 (0) | 2024.12.23 |
[Spring] Spring Events 이란? (2) | 2024.12.22 |