인프런 커뮤니티 질문&답변

kas0320님의 프로필 이미지

작성한 질문수

자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]

강의 소개 영상

26강 코딩 후 컨파일 시 오류 문의드립니다 !

23.02.16 22:31 작성

·

804

1

 Description:

The bean 'userRepository', defined in com.group.libraryapp.domain.user.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in class path resource [com/group/libraryapp/config/UserConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

delete 부분까지 코딩 후에 테스트 진행하려고 , 컴파일 시에 위와 같은 오류가 발생하고 있습니다.
구글링으로 아래 action 부분도 해보았으나 다른 오류들이 발생하네요 ㅜㅜ

답변 1

0

최태현님의 프로필 이미지
최태현
지식공유자

2023. 02. 17. 23:32

안녕하세요! kas0320님!! 질문 올려주셔서 감사드립니다!! 🙏

보내주신 내용을 확인해본 결과, UserRepositorybean으로 동시에 2개 등록되려 한 것 같습니다!

 

어떤 말씀이냐면요!!

public interface UserRepository extends JpaRepository<User, Long> {

}

라는 코드는 UserRepository 를 빈으로 등록합니다!! (즉, UserRepository를 스프링 컨테이너에 넣으려고 합니다)

그리고 아마 어딘가에 다음과 같은 코드도 있을거에요!!!

@Configuration
public class UserConfiguration {
  @Bean
  public UserRepository userRepository() {
    return new UserRepository(); // 정확하진 않더라도 비슷한 코드가 있을겁니다!
  }
}

이 코드 역시 UserRepository 를 빈으로 등록하려 합니다!

 

바로 여기서, 문제가 발생합니다!!! 진작 UserRepository 가 빈으로 존재하는데 UserConfiguration 에 의해 또 하나의 UserRepository 가 등록되려 하는 것이죠!

그래서 에러가 이렇게 말하고 있습니다.

  • The bean 'userRepository', defined in com.group.libraryapp.domain.user.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in class path resource [com/group/libraryapp/config/UserConfiguration.class] and overriding is disabled.

  • (= 그 이름을 갖는 Bean은 진작 존재하고 있다)

이 문제를 해결하기 위해서는 UserConfiguration 을 제거해주시면 됩니다!

 

꼭 해결되셨으면 좋겠습니다! 😊

감사합니다!! 🙇

kas0320님의 프로필 이미지

작성한 질문수

질문하기