[Spring] dispatcher-servlet.xml?

김진서·2024년 3월 1일

Spring MVC

목록 보기
5/9
post-thumbnail

dispatcher-servlet.xml

dispatcher-servlet.xml은 Spring MVC 웹 애플리케이션에서 사용되는 설정 파일. 이 파일은 Spring의 DispatcherServlet에 의해 로드되어 애플리케이션의 웹 계층을 구성.

dispatcher-servlet.xml 파일에는 주로 다음과 같은 내용이 포함될 수 있다:

  1. View Resolver 설정: 주로 InternalResourceViewResolver와 같은 뷰 리졸버를 설정하여 컨트롤러가 반환한 뷰 이름을 실제 JSP 파일과 매핑.
  2. Handler Mapping 설정: 컨트롤러 클래스와 요청 URL을 매핑하는 핸들러 매핑을 설정.
  3. Interceptor 설정: 요청을 가로채고 처리 전후에 추가 작업을 수행하는 인터셉터를 설정.
  4. Multipart Resolver 설정: 파일 업로드와 같은 멀티파트 요청을 처리하는 데 사용되는 멀티파트 리졸버를 설정.
  5. Message Converter 설정: HTTP 요청 및 응답 데이터를 객체로 변환하고 반대로 객체를 HTTP 응답으로 변환하는 메시지 컨버터를 설정.
  6. Exception Resolver 설정: 애플리케이션에서 발생한 예외를 처리하고 적절한 오류 페이지를 표시하는 예외 리졸버를 설정.
  7. Handler Adapter 설정: 요청을 처리하기 위해 사용되는 핸들러 어댑터를 설정.
  8. 기타 필요한 웹 계층 구성 요소들을 설정.

-> dispatcher-servlet.xml은 Spring MVC의 핵심 설정 파일 중 하나이며, 애플리케이션의 웹 계층을 정의하는 데 사용. 이 파일은 주로 Spring MVC 프레임워크의 다양한 기능과 구성 요소들을 선언하고 구성.

일반적인 Spring 프로젝트 dispatcher-servlet.xml (+ JPA)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <mvc:annotation-driven/>
    <!-- 트랜잭션 관리 설정 -->
    <tx:annotation-driven />
    <context:component-scan base-package="org.example"/>
    <!-- JPA 사용 설정 -->
    <jpa:repositories base-package="org.example.repository" />

    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

각 요소는 다음과 같은 역할을 수행:

  1. xmlnsxsi:schemaLocation: XML 네임 스페이스 및 스키마 위치를 정의.
  2. <mvc:annotation-driven/>: Spring MVC 어노테이션 기반 컨트롤러를 활성화. 이를 통해 @Controller, @RequestMapping 및 기타 MVC 어노테이션을 사용하여 컨트롤러를 정의할 수 있다.
  3. <tx:annotation-driven />: Spring의 어노테이션 기반 트랜잭션 관리를 활성화. @Transactional 어노테이션을 사용하여 트랜잭션을 정의할 수 있다.
  4. <context:component-scan base-package="org.example"/>: 지정된 패키지와 하위 패키지에서 Spring 컴포넌트를 검색하고 등록. @Component, @Controller, @Service, @Repository 등의 어노테이션이 있는 클래스를 찾습니다.
  5. <jpa:repositories base-package="org.example.repository" />: Spring Data JPA 리포지토리를 활성화하고 스캔할 패키지를 지정. 이를 통해 Spring은 JpaRepository 인터페이스를 구현한 리포지토리 빈을 자동으로 생성.
  6. <mvc:resources mapping="/resources/**" location="/resources/"/>: 정적 리소스(이미지, CSS, JavaScript 등)를 제공하기 위해 리소스 핸들러를 등록. 예를 들어, /resources/** URL 패턴에 매핑된 요청은 /resources/ 디렉토리에서 처리.
  7. InternalResourceViewResolver Bean: JSP 뷰 파일을 해석하고 렌더링하기 위한 ViewResolver를 정의. 이 Bean은 JSP 파일이 저장된 경로 및 확장자를 지정.

정리
dispatcher-servlet.xml: Spring MVC, 트랜잭션 관리, 데이터 액세스와 같은 핵심 기능을 설정하고 웹 애플리케이션의 구성을 구성하는 데 사용.

profile
PAy IT forwaRD를 실천하는 프론트엔드 개발자. 근데 이제 백엔드를 곁들인..

0개의 댓글