설정을 위한 설정파일이다. 배포 기술자로써 영어로는 DD(Deployment Descriptor) 이다.
이 파일은 WAS(Web Application Server)가 최초 구동될 때 즉 톰켓이 최초 구동될 때 web.xml을 읽고 그에 해당하는 설정을 구성한다. 즉 각종 설정을 위한 설정파일이라고 할 수 있다.
<?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 http://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> - 루트 컨텍스트로 모든 서블릿과 필터들이 공유함. root-context.xml을 정의
<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>
<!-- Processes application requests -->
<servlet> - 서블릿 설정
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> - DispatcherServlet으로 앞단에서 요청정보를 핸들링 해줌.
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> -servlet-context.xml을 가르키고 있음.
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping> - appServlet에 대한 url-pattern을 정의
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2) servlet-context.xml
주석을 보면 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 이라고 나와있다.
해석해보면 DispatcherServlet Context : 이 서블릿의 요청처리 인프라를 정의한다. 이다. Dispatcher 서블릿과 관련된 설정을 하는것 같다.
정답은 아니지만 주로 View 지원 bean을 설정한다고 한다. ex) Controller
그래서 그런지 어노테이션, 리소스 디렉토리, ViewResolver에 관한 설정들이 있다.
<?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.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/" /> - HTML 리소스 디렉토리 정의
<!-- 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"> - ViewResolver로 jsp와 name 을 매핑
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.hee.heechart" /> - 베이스 패키지 하위 모든 어노테이션을 스캔해서 빈으로 등록하겠다는 것.
</beans:beans>
3) root-context.xml
처음에 프로젝트 생성시에는 아무 내용도 없다. 이곳은 공통빈을 설정하는 곳으로 주로 View 지원을 제외한 bean을 설정한다고 한다.
ex) Service / Repository(DAO) / DB/ log 등등
mybatis를 root-context에 만들어 보도록 하자.
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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">
<!-- Root Context: defines shared resources visible to all other web components -->
<!--먼저 오라클 드라이버 연결 spring에서는 객체를 bean으로 생성한다. xml에서 bean으로 생성하는 방법이있고, annotation으로 생성하는 방법이 있고, config로 생성하는 방법이 있다.-->
<bean name="driver"
</beans>