[spring]Error creating bean with name 'XXX'

홍헌·2023년 9월 18일
0

spring

목록 보기
2/7
post-thumbnail

해당 에러는 구글링 했을 때 몇 가지 해결방법을 비교적 쉽게 찾을 수 있다.
나 또한 그랬고 내 경우 mapper 작성을 잘못한 경우였는데 혹시 오타가 정말 안 보일 경우 이런 것도 의심해 봤으면해서 작성한다.

읽어봤을 때 sql문 자체는 이상이 없었으나 내가 작성한 것이라서 그런지 정말 오타를 찾기 어려웠다.
뭔지 찾으셨다면 진짜 그 눈 사고싶을 거 같은데 여기서 오타는 resultType이었다. paraMeterType을 작성하던 게 습관이 돼서 무의식적으로 resultMap을 잘못 작성한 것이었다.

해당 에러 발생 시, 꼭 mapper를 잘 보길 바라며...



<------------------2023.09.20 수정------------------>

위의 글을 쓰고 보니 resultMap 사용하려면 resultMap을 선언하지 않았는데 왜 잘 돌아가지 라는 생각이 들었고, resultMap을 미리 설정하고 다시 돌려보았더니 또 돌아가지 않았다. 다시 resultType으로 변경하고 mybatis-config파일에 alias를 다시 설정하였는데 동일한 문제가 발생하였다.

그래서 커뮤니티에 질문도 남겨보고 글도 찾아봤는데 다 해결이 되지 않아 스스로 해결해야한다는 생각이 들어 에러를 부분적이 아니라 천천히 다 읽어봤다.
에러 첫 번째 줄을 다 복사하면 아래와 같다.

<에러>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testDAOMyBatis': Unsatisfied dependency expressed through field 'sqlSession'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSession' defined in spring.conf.SpringConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.SqlSessionTemplate]: Factory method 'sqlSession' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in spring.conf.SpringConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [com/myMbti/dao/testMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'class path resource [com/myMbti/dao/testMapper.xml]'. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for testSQL.getData. please check com/myMbti/dao/testMapper.xml and class path resource [com/myMbti/dao/testMapper.xml]

읽다가 내가 주목한 부분은 마지막 부분이었다. 해당 에러는 mapper에서 id가 중복값으로 존재할 때 발생하는 문제거나 sql문에서 뭔가 잘못됐을 때 발생하는 문제라고했다. 근데 아무리 봐도 내 sql문에는 이상이 없었다. 테스트용 mapper에 존재하는 sql문은 아래와 같다.


아무리 봐도 이상이 없었는데 문득 든 생각이 "mapper가 중복으로 존재하나?"라는 생각이었다.

그 이유는 mybatis-config에 mapper를 설정해뒀는데

root-context에서 dao를 스캔하게 해뒀고 내 dao에는 mapper 파일이 같이 있었기 때문이었다.

그래서 config파일에서 mapper를 주석처리하니 제대로 돌아갔다.

결론 : 2일동안 난 뭘했나... 에러 잘 읽고 설정 잘하자...

0개의 댓글