1.
톰캣을 실행하기 전 톰캣의 설정 파일인 web.xml 파일에 <context-param>으로 전역 파라미터를 설정한다.
파라미터의 이름은 contextConfigLocation이고, 어떤 객체들을 미리 만들어 놓을지 작성된 설정 파일의 경로를 값으로 할당 받는다.
<!-- 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>
2.
톰캣이 실행되면 수행되어야 할 클래스(리스터)의 이름을 web.xml에 작성한다. (리스너란?)
톰캣이 실행하면 <listener>에 등록되어 있는 ContextLoaderListener 객체를 호출하는데, 이 객체는 내부적으로 부모 객체를 실행한다.
부모 객체는 ContextLoader이며 이 객체에서 RootWebApplicationContext를 구성하게 되는데 그 설정파일은 root-context.xml이다.
이 컨테이터에는 웹과 관련 없는 객체들을 저장한다. 예를들면 DAO객체로 웹과 직접적인 관련이 없는 객체이다. 단지, DB에 접근하기 위한 객체일 뿐이다.
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.
RootWebApplicationContext 컨테이너에 객체들을 구성하기 위해서 (1)에서 작성한 전역 파라미터 이름인 contextConfigLocation의 값으로 설정 된 /WEB-INF/spring/root-context.xml 의 경로로 가서 읽는다.
public class ContextLoader {
/**
* Config param for the root WebApplicationContext id,
* to be used as serialization id for the underlying BeanFactory: {@value}
*/
public static final String CONTEXT_ID_PARAM = "contextId";
/**
* Name of servlet context parameter (i.e., {@value}) that can specify the
* config location for the root context, falling back to the implementation's
* default otherwise.
* @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
*/
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
ContextLoader 클래스에는 CONFIG_LOCATION_PARAM이라는 상수가 정의되어 있는데, 기본 값으로 문자열 contextConfigLocation이 할당되어 있다.
그래서 (1)에서 전역 파라미터 이름을 contextConfigLocation으로 작성한 것이다.
그리고 contextConfigLocation는 사실, 설정 파일 경로 /WEB-INF/spring/root-context.xml 를 의미한다.
/WEB-INF/spring/root-context.xml 은 어떤 객체들을 미리 생성해 놓을지 정의한 설정 파일이다.
즉, ContextLoaderListener 리스터를 통해 root-context.xml 을 읽고, RootWebApplicationContext 컨테이너에 웹과 관련 없는 객체들이 생성된다.
4. 더 공부가 필요한 부분 - 추후 정리
※ 여기까지가 톰캣 start 버튼을 눌렀을 때 수행되는 과정이다.
개발자가 해야할 일은 설정파일을 등록하는 것과 어노테이션을 추가하는 것 밖에 없다.
설정만 잘 해주면 스프링이 알아서 객체들을 생성해주고 관리 해준다.