Recent Posts
Recent Comments
Link
06-30 12:53
Today
Total
관리 메뉴

삶 가운데 남긴 기록 AACII.TISTORY.COM

Spring MVC 프로젝트 구조 본문

DEV&OPS/Java

Spring MVC 프로젝트 구조

ALEPH.GEM 2023. 1. 19. 20:34

시퀀스 다이어그램

 

 

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)

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