KOSA Spring - component-scan 차이 정리

채정윤·2025년 4월 17일

Spring

목록 보기
7/25
항목servlet-context.xmlroot-context.xml
역할웹 계층(Web Layer) 설정서비스 계층 및 비즈니스 로직 설정
스캔 대상 컴포넌트@Controller, @RestController@Service, @Repository, @Component
주요 목적웹 요청 처리 담당 (Spring MVC DispatcherServlet에서 사용)비즈니스 로직 및 데이터 접근 계층 관리
스프링 MVC와의 관계DispatcherServlet 전용 설정Root Application Context 설정
로드 시점DispatcherServlet 초기화 시 로드웹 애플리케이션 시작 시 함께 로드
추천 스캔 패키지com.example.project.controllercom.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 같은 패키지를 스캔


참고))

  • DispatcherServlet은 웹 앱 시작 시 등록되고 초기화됨
  • web.xml 에서 등록
  • 등록하면서 설정 파일(servlet-context.xml) 연결

0개의 댓글