Spring: Dispacher Servlet 과 ContextLoader Listener 의 WebApplication Context 의 관계

웹 용기 (예 를 들 어 Tomcat) 에서 Spring 을 설정 할 때 웹. xml 파일 의 다음 설정 코드 에 익숙 할 수 있 습 니 다.
 
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
                                                                                                                                             
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
                                                                                                                                             
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
                                                                                                                                         
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 
이 설정 은 먼저 ContextLoaderListener 에서 < context - param > 의 applicationContext. xml 를 통 해 applicationContext 를 만 든 다음 이 applicationContext 를 ServletContext 에 넣 고 ServletContext 의 setAttribute 방법 으로 이 목적 을 달성 합 니 다. ContextLoaderListener 의 소스 코드 에서 우 리 는 이러한 코드 를 볼 수 있 습 니 다.
 
servletContext.setAttribute(
              WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
 this.context);

 
이상 ContextLoader Listener 가 만 든 applicationContext 는 전체 웹 프로그램 에 공 유 됩 니 다. Dispatcher Servlet 은 자신의 application Context 를 유지 할 것 이라는 것 을 이미 알 고 있 었 을 것 입 니 다. 기본 값 은 / WEB - INFO / < dispatcher ServletName > - servlet. xml 파일 을 읽 을 것 입 니 다. 저희 도 다시 설정 할 수 있 습 니 다.
 
<servlet>
    <servlet-name>
       customConfiguredDispacherServlet
   </servlet-name>
   <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
   </servlet-class>
   <init-param>
       <param-name>
           contextConfigLocation
       </param-name>
       <param-value>
           /WEB-INF/dispacherServletContext.xml
       </param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

 
문 제 는 상기 두 응용 프로그램 Context 의 관 계 는 무엇 입 니까? 그들의 역할 범 위 는 각각 무엇 입 니까? 그들의 용 도 는 각각 무엇 입 니까?
 
ContextLoader Listener 에서 applicationContext 를 만 드 는 것 은 전체 웹 응용 프로그램 이 공유 해 야 할 구성 요소, 예 를 들 어 DAO, 데이터 뱅 크 의 Connection Factory 등 입 니 다.Dispatcher Servlet 에서 만 든 applicationContext 는 주로 이 Servlet 과 관련 된 일부 구성 요소, 예 를 들 어 Controller, ViewResovler 등 입 니 다.
 
역할 범위 에 있어 서 Dispatcher Servlet 에 서 는 ContextLoader Listener 가 만 든 applicationContext 를 참조 할 수 있 으 며, 반대로 안 됩 니 다.
 
Spring 의 구체 적 인 실현 에 있어 이 두 응용 프로그램 Context 는 모두 ServletContext 의 setAttribute 방법 을 통 해 ServletContext 에 넣 은 것 이다.단, ContextLoaderListener 는 Dispatcher Servlet 보다 먼저 Application Context 를 만 들 고, Dispatcher Servlet 는 Application Context 를 만 들 때 ContextLoader Listener 가 만 든 Application Context 를 먼저 찾 은 다음, 후자 의 Application Context 를 매개 변수 로 Dispatcher Servlet 에 전달 하 는 Application Context 의 setParent () 방법 은 Spring 소스 코드 에서FrameServlet. 자바 에서 다음 코드 를 찾 을 수 있 습 니 다.
 
wac.setParent(parent);

 
그 중에서 wac 는 DisptcherServlet 을 이유 로 만 든 applicationContext 이 고 parent 는 ContextLoader Listener 가 만 든 applicationContext 입 니 다.이후 프레임 워 크 는 ServletContext 의 setAttribute () 방법 으로 wac 를 ServletContext 에 추가 합 니 다.
 
Spring 이 응용 프로그램 Context 의 getBean 을 실행 할 때, 자신의 context 에서 대응 하 는 bean 을 찾 지 못 하면 부모 응용 프로그램 Context 에서 찾 습 니 다.이것 은 우리 가 Dispatcher Servlet 에서 ContextLoader Listener 에 대응 하 는 Application Context 의 bean 을 얻 을 수 있 는 이 유 를 설명 한다.
참고:http://www.davenkin.me/post/2012-10-18/40039948363

좋은 웹페이지 즐겨찾기