에러일기

[SpringBoot] org.springframework.beans.factory.BeanDefinitionStoreException - Controller Naming 에러 해결

YooKyungHun 2023. 3. 18. 22:57

에러 메시지

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.jscode.spring.Application];
nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'productController' for bean class [com.jscode.spring.day04.Example2.ProductController] conflicts with existing,
non-compatible bean definition of same name and class [com.jscode.spring.day04.Example1.ProductController]

 

문제발생

 

원인 및 해결책

Example1 package 의 ProductController 와
Example2 package 의 ProductController 의 이름 중복

 

spring 은 annotation-driven component scan 시 기본적으로 Bean 이름으로 id 를 결정한다.

예를 들어서 클래스명이 TestController 라면 id는 “testController” 로 결정된다.

이때 다른 패키지에서 동일한 이름을 가진 클래스(컴포넌트)가 중복으로 존재한다면,

동일한 id 를 가지게 되며 위와 같은 에러 메시지를 볼 수 있다.

 

Example2 package 의 Controller 이름을 바꿔주었다.

비슷한 내용의 Controller 여서 하나의 Controller 에 내용을 다 작성할 수도 있었지만

일부러 문제마다 코드를 명확히하려고 Example1, 2 로 나눠놓았던 것이 원인이 되겠다.

 

 

기타

spring 에서 기본 제공하는 BeanNameGenerator 를 커스터마이징 할 수도 있었지만,

굳이 그렇게까지는 하지 않았다.

 

spring annotation-driven 사용시 bean name 중복(충돌,conflicts) 해결

spring은 annotation-driven component scan 시 기본적으로 Bean 이름으로 id를 결정한다. 이때 동일한 이름의 클래스(컴포넌트)가 존재한다면 동일한 id 를 가지게 되고 'conflicts with existing' 오류가 발생한다. B

itnp.kr