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

얼티밋님의 프로필 이미지

작성한 질문수

[NarP Series] MVC 프레임워크는 내 손에 [나프2탄]

38_Spring WEB MVC 동작원리(2)

root-context.xml에 context:component-scan을 넣을시 에러가 발생합니다.

22.12.07 01:08 작성

·

595

0

root-context.xml에<context:component-scan base-package="kr.bit.model" />을 넣을시 ERROR: org.springframework.web.context.ContextLoader - Context initialization failed java.lang.ArrayIndexOutOfBoundsException: 40292가 발생하여 구글에서 검색해본결과 servlet-context.xml 에<context:component-scan base-package="kr.narp.myapp, kr.bit.model" />식으로 정의하여 해결했습니다. context:component-scan 을 여러개 할시 에러가 발생하는듯한데 이게 원인일까요?

 

답변 1

1

박매일님의 프로필 이미지
박매일
지식공유자

2022. 12. 08. 11:56

context:component-scan은 여러 개를 해도 상관은 없습니다.

servlet-context.xml에서 scan된 객체는 root-context.xml에서 정의된 객체를 사용할 수가 있는데
반대로 root-context.xml에서 scan된 객체는 servlet-context.xml에서 정의된 객체를 사용할 수가 없습니다. 그래서 database객체는 root-context.xml에서 scan하고
controller나 service는 servlet-context.xml에 생성하는 것이 바람직합니다.