0. 배경
STS를 사용해 Spring 프로젝트를 만들었다.
이때 web.xml, root-context.xml, servlet-context.xml 과 같은 설정 파일들을 관리해야 했다.
각각 어떤 설정을 하는 것인지, 어떤 순서로 동작하는지 알아보자.
STS란?
Spring 프레임워크를 위한 공식 IDE로, Eclipse 기반으로 개발되었다.
1. web.xml
- 웹 애플리케이션의 Deployment Descriptor이다.
- 웹 애플리케이션의 구성 요소와 서블릿 컨테이터가 애플리케이션을 어떻게 관리하고 실행할지에 대한 정보를 담고 있는 설정 파일이다.
- 이 파일은 웹 애플리케이션을 톰캣과 같은 서블릿 컨테이너에 배포할 때, 서블릿 컨테이너가 처음 읽는 파일이다.
- 서블릿, 필터, 리스너 등 웹 애플리케이션 컴포넌트에 대한 구성 정보를 포함한다.
- 애플리케이션에 사용되는 서블릿을 선언하고, URL 패턴에 따라 어떤 서블릿이 호출될지 매핑 정보를 제공한다.
- Spring에서는 'DispatcherServlet'의 초기화와 관련 설정을 포함한다.
2. root-context.xml
- 애플리케이션 전반에 걸쳐 사용되는 Bean 설정을 담당하는 파일이다.
- `ContextLoaderListener`를 통해 로드되며, 이 리스너는 web.xml에서 설정된다.
- 데이터베이스 연결 정보와 같은 데이터 소스를 설정한다. (Connection Pool, JDBC DataSource 설정 등)
- 트랜잭션 관리
- MyBatis, JPA 등의 ORM 설정
- AOP 설정 등
3. servlet-context.xml
- 웹 애플리케이션에서 웹 요청과 관련된 설정을 담당하는 파일이다.
- 특히 DispatcherServlet에 대한 구성이 주로 포함되어 있다.
- ViewResolver, Handler Mapping, Controller 및 ControllerScan, Interceptor 등과 관련된 설정을 한다.
4. 동작 순서
(1) 웹 애플리케이션 시작 시, 서블릿 컨테이너는 web.xml을 읽는다.
(2) web.xml 내에 정의된 ContextLoadListener가 실행되어 root-context.xml을 로드하고, 애플리케이션의 전반적인 Bean을 생성 및 관리한다.
(3) web.xml에 정의된 DispatcherServlet이 초기화되며, 이때 servlet-context.xml이 로드된다.
(4) 사용자 요청이 들어올 때, DispatcherServlet이 해당 요청을 처리하고, 필요한 컨트롤러와 뷰를 매핑하며 응답을 반환한다.
web.xml
root-context.xml과 servlet-context.xml을 로드하고 있다.
url-pattern을 정의한다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 웹 아닌 설정 -->
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- 리스너 등록 -->
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- POST 방식의 한글 처리. -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 웹 관련 설정 -->
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name> <!-- 서블릿 이름 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <!-- 서블릿이 만들어질 때 초기 파라미터를 넘겨줌, 그에 대한 정보 xml에 있음 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern> <!-- 서블릿 이름에 대해 경로 매핑 -->
</servlet-mapping>
</web-app>
root-context.xml
bean 설정 정보를 담고 있다.
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:component-scan base-package="com.ssafy.mvc.model"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssafyws?serverTimezone=UTC" />
<property name="username" value="ssafy" />
<property name="password" value="ssafy" />
</bean>
<!--
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssafyws?serverTimezone=UTC" />
<property name="username" value="ssafy" />
<property name="password" value="ssafy" />
</bean>
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mappers/**/*.xml" />
<property name="typeAliasesPackage" value="com.ssafy.mvc.dto" />
</bean>
<mybatis-spring:scan base-package="com.ssafy.mvc.model.repo"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--<constructor-arg ref="driverClassName"></constructor-arg> -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- <tx:annotation-driven transaction-manager="tm" /> -->
<tx:annotation-driven />
</beans>
servlet-context.xml
ViewResolver 관련 설정을 통해 컨트롤러에서 반환된 뷰 이름을 실제 뷰 파일로 매핑하고 있다.
ComponentScan을 어디서부터 할지 지정하고 있다.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->
<resources mapping="/img/**" location="/resources/img/" />
<resources mapping="/css/**" location="/resources/css/" />
<resources mapping="/js/**" location="/resources/js/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.ssafy.mvc.controller" />
</beans:beans>
'Spring' 카테고리의 다른 글
[Spring Boot] 5. 간단한 POST API를 만든다(1/2) (0) | 2023.10.23 |
---|---|
[Spring Boot] 4. 간단한 GET API를 만든다(3/3) (1) | 2023.10.17 |
[Spring Boot] 3. 간단한 GET API를 만든다(2/3) (0) | 2023.10.14 |
[Spring Boot] 2. 간단한 GET API를 만든다(1/3) (1) | 2023.10.09 |
[Spring Boot] 1. 스프링 부트 프로젝트를 만든다 (0) | 2023.10.01 |