2

web.xml フラグメント:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-security.xml</param-value>
  </context-param>

  <!-- Processes application requests -->
  <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>

この答えによると:

2- DispatcherServlet コンテキストはルート コンテキストの子になります。...

私の質問は、Spring がこの決定を行う方法を理解することです (DispatcherServlet コンテキストをルートコンテキストにアタッチする)。これを指定するappContext XMLファイルには明示的なものは何もありません。また、この関連付けを明示的に行うためにXMLで指定できるものは何もありません。

DispatcherServlet がその appContext をインスタンス化するとき、それを呼び出すことをどのように認識しますかsetParent()(SpringMVC はルート appContext がなくても問題なく動作します)、子以外の appContext が複数存在する場合、どのように選択しますか?

4

2 に答える 2

2

DispatcherServlet は、次のコードを持つ FrameworkServlet を拡張します。

    protected WebApplicationContext initWebApplicationContext() {
        WebApplicationContext rootContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            WebApplicationContext wac = null;

その後、次のように使用されます。

 wac = createWebApplicationContext(rootContext);

WebApplicationContextUtils はコンテキストを次のように取得します。

public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
    return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
}

public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) {
    Assert.notNull(sc, "ServletContext must not be null");
    Object attr = sc.getAttribute(attrName);
            ...
    return (WebApplicationContext) attr;
}

簡単に言うと、ルート コンテキストはそれ自体への参照をコンテキスト属性に格納します。すべての FrameworkServlets は、前述のコンテキスト属性を取得し、取得したルート コンテキスト (定義されている場合) にバインドしようとします。

于 2013-02-01T18:30:00.097 に答える
1

SpringMVC ApplicationContext 階層 -

ApplicationContext (I ) -ConfigurableApplicationContext (I
     )
     -WebApplicationContext (I)

于 2014-11-09T09:02:11.270 に答える