삶 가운데 남긴 기록 AACII.TISTORY.COM
Spring MVC 프로젝트 구조 본문

Spring MVC 프로젝트 구조 설명
- src/main/java/ : 자바 소스 경로
- src/main/resources/ : 실행시 자동 참고되는 경로(주로 설정파일, log4j.xml 등등)
- src/test/java/ : 테스트 자바 코드 경로
- src/test/resources/ : 테스트 관련 설정 파일 경로
- src/webapp/WEB-INF/spring/appServlet/ : sevlet-context.xml 외 spring 설정 파일
- src/webapp/WEB-INF/spring/ : root-context.xml 외 spring 설정 파일
- src/webapp/WEB-INF/views/ : MVC 패턴 중 view 페이지(jsp) 들이 위치 한 경로
- src/webapp/WEB-INF/ : tomcat의 web.xml 파일 위치
DispatcherServlet (FrontController)
기존 웹 어플리케이션 개발은 HttpServlet을 상속하는 클래스를 만들고 doGet()이나 doPost()메서드를 구현하고 HttpServletRequest에서 매개변수를 추출하고 비지니스 로직(process)를 실행하고 반환될 정보를 HttpServletResponse에 담아서 반환합니다.
이 때, 비지니스 로직을 제외하고 나머지 작업들은 반복해서 해야하는 작업이기 때문에 이 반복작업을 대신 해주는 서블릿이 DispatcherServlet입니다.
DispatcherServlet은 web.xml 에서 등록합니다.
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<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>
DispatcherServlet은 WebApplicationContext를 생성합니다.
WebApplicationContext는 bean 관리, Controller, HandlerMapping, ViewResolver 를 포함합니다.
WebApplicationContext
어플리케이션 전체 관련 bean은 root-context.xml에서 관리합니다.
데이터 소스 bean, 트랜잭션 bean, 서비스 bean, VO bean 등을 등록합니다.
web.xml에 ContextLoaderListener는 어플리케이션 컨텍스트를 시작하고 종료시킵니다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
root-context를 등록하기 위해서는 web.xml에 이벤트 리스너를 등록해야 합니다.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
servlet-context.xml
서블릿 관련 bean은 servlet-context.xml 에서 관리합니다.
컨트롤러, 핸들러 매핑, 뷰리졸버 등을 관리하는 bean 을 등록 합니다.
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- 어노테이션 와이어링 -->
<annotation-driven />
<!-- URL로 오는 모든 정적인 리소스 파일들 경로 -->
<resources mapping="/resources/**" location="/resources/" />
<!-- 뷰 리졸버 -->
<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.aacii.order" />
</beans:beans>
728x90
'DEV&OPS > Java' 카테고리의 다른 글
| Spring 주요 어노테이션 (0) | 2025.11.05 |
|---|---|
| Spring bean (0) | 2025.11.05 |
| Spring 의존성 주입과 제어의 역전 (1) | 2025.11.04 |
| 리스너(Listener) : 옵저버(observer) 패턴 (0) | 2025.11.03 |
| Singleton Pattern 과 DeadLock (22) | 2025.11.01 |