| 항목 | servlet-context.xml | root-context.xml |
|---|---|---|
| 역할 | 웹 계층(Web Layer) 설정 | 서비스 계층 및 비즈니스 로직 설정 |
| 스캔 대상 컴포넌트 | @Controller, @RestController | @Service, @Repository, @Component |
| 주요 목적 | 웹 요청 처리 담당 (Spring MVC DispatcherServlet에서 사용) | 비즈니스 로직 및 데이터 접근 계층 관리 |
| 스프링 MVC와의 관계 | DispatcherServlet 전용 설정 | Root Application Context 설정 |
| 로드 시점 | DispatcherServlet 초기화 시 로드 | 웹 애플리케이션 시작 시 함께 로드 |
| 추천 스캔 패키지 | com.example.project.controller | com.example.project.service, com.example.project.repository |
| 예시 설정 | <context:component-scan base-package="com.example.web.controller" /> | <context:component-scan base-package="com.example.service, com.example.repository" /> |
servlet-context.xml
주로 웹 계층, 즉 컨트롤러 관련 설정을 담당
그래서 @Controller나 @RestController와 같은 웹 요청을 처리하는 컴포넌트들을 스캔
이 파일은 DispatcherServlet이 초기화될 때 로드되며, 예를 들어 com.example.web.controller 같은 컨트롤러 패키지를 스캔
root-context.xml
비즈니스 로직이나 데이터 처리와 관련된 설정을 담당
여기서는 @Service, @Repository, @Component 같은 컴포넌트를 스캔하며, 일반적으로 웹 애플리케이션이 시작될 때 함께 로드
예를 들어 서비스나 DAO 관련된 com.example.service, com.example.repository 같은 패키지를 스캔
참고))
web.xml 에서 등록servlet-context.xml) 연결