Debug

[SpringBoot] A bean with that name has already been defined 에러

배게 2021. 7. 15. 12:48
728x90

Description: 
The bean 'memberService', defined in class path resource [hello/hellospring/SpringConfig.class], could not be registered. A bean with that name has already been defined in file [C:\Users\Donald.Lee\IdeaProjects\hello-spring\out\production\classes\hello\hellospring\service\MemberService.class] and overriding is disabled. 
Action: 
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true 

Process finished with exit code 1 

-> 네 오류 메시지를 잘 남겨주셨네요^^ 

메시지를 분석해보면 MemberService가 이미 스프링 빈으로 등록되어서 SpringConfig에 정의한 memberService 빈 등록 설정을 적용할 수 없다는 오류입니다. 
컴포넌트 스캔과 자동 의존관계 설정에서는 다음과 같이 코드를 적용했습니다. 

@Service 
public class MemberService {} 
자바 코드로 직접 스프링 빈 등록하기에서는 다음과 같이 @Service를 제거했습니다. 

//@Service -> 이 라인을 지워주세요 
public class MemberService {} 
이렇게 @Service를 제거해주시면 컴포넌트 스캔의 대상에서 제외되기 때문에, 이후부터는 직접 빈 등록을 진행하셔도 될꺼에요. 
감사합니다. 

(그니까 '컴포넌트 스캔과 자동 의존관계 설정'으로 이미 박았는데 '자바 코드로 직접 스프링 빈 코드 등록'으로 또 박으려고 하니까 컴파일러가 더는 못해먹겠다고 하는 것 같음)