Spring: Dispacher Servlet 과 ContextLoader Listener 의 WebApplication Context 의 관계
<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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.