<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # `ContextLoaderListener`與`DispatcherServlet` > 原文: [https://howtodoinjava.com/spring-mvc/contextloaderlistener-vs-dispatcherservlet/](https://howtodoinjava.com/spring-mvc/contextloaderlistener-vs-dispatcherservlet/) 在基于 XML 的 Spring MVC 配置中,您必須在`web.xml`文件中看到兩個聲明,即`ContextLoaderListener`和`DispatcherServlet`。 讓我們嘗試了解它們在框架中的用途及其差異。 ## 根和子上下文 在進一步閱讀之前,請了解: * Spring 一次可以有多個上下文。 其中之一將是根上下文,而所有其他上下文將是子上下文。 * 所有子上下文都可以訪問在根上下文中定義的 Bean。 但事實并非如此。 根上下文無法訪問子上下文 Bean。 ## `DispatcherServlet` – 子應用程序上下文 [`DispatcherServlet`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html)本質上是 Servlet(它擴展了`HttpServlet`),其主要目的是處理與配置的 URL 模式匹配的傳入 Web 請求。 它采用傳入的 URI 并找到控制器和視圖的正確組合。 因此它是前端控制器。 在 spring 配置中定義`DispatcherServlet`時,您將使用`contextConfigLocation`屬性為 XML 文件提供控制器類,視圖映射等條目。 `web.xml` ```java <servlet> <servlet-name>employee-services</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:employee-services-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ``` 如果不提供配置文件,則它將使用`[servlet_name]-servlet.xml`加載其自己的配置文件。 Web 應用程序可以定義任意數量的`DispatcherServlet`條目。 每個 servlet 將在其自己的名稱空間中運行,并使用映射,處理器等加載其自己的應用程序上下文。 這意味著每個`DispatcherServlet`都可以訪問 Web 應用程序上下文。 **在指定之前,每個`DispatcherServlet`都會創建自己的內部 Web 應用程序上下文**。 從Spring 3.x開始,方法`DispatcherServlet(WebApplicationContext webApplicationContext)`使用給定的 Web 應用程序上下文創建一個新的`DispatcherServlet`。 只有通過`ServletContext.addServlet(java.lang.String, java.lang.String)`API支持,才可以在 Servlet 3.x 環境中使用。 ## `ContextLoaderListener` – 根應用程序上下文 [**`ContextLoaderListener`**](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/ContextLoaderListener.html)創建根應用程序上下文,并將與所有`DispatcherServlet`上下文創建的子上下文共享。 在`web.xml`中只能有一個條目。 `web.xml` ```java <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </context-param> ``` `ContextLoaderListener`的上下文包含全局可見的 Bean,例如服務,存儲庫,基礎結構 Bean 等。創建根應用程序上下文后,它作為屬性存儲在`ServletContext`中,名稱為: `org/springframework/web/context/ContextLoader.java` ```java servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); //Where attibute is defined in /org/springframework/web/context/WebApplicationContext.java as WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT"; ``` 要在 Spring 控制器中獲取根應用程序上下文,可以使用`WebApplicationContextUtils`類。 `Controller.java` ```java @Autowired ServletContext context; ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context); if(ac == null){ return "root application context is null"; } ``` ## `ContextLoaderListener`與`DispatcherServlet` 下圖在單個視圖中描述了整個關系。 ![ContextLoaderListener vs DispatcherServlet](https://img.kancloud.cn/35/7f/357f2bb5338f367d70057669a9c4c955_1398x906.jpg) `ContextLoaderListener` vs `DispatcherServlet` 1. `ContextLoaderListener`創建根應用程序上下文。 2. `DispatcherServlet`條目為每個 Servlet 條目創建一個子應用程序上下文。 3. 子上下文可以訪問在根上下文中定義的 bean。 4. 根上下文中的 Bean 無法(直接)訪問子上下文中的 Bean。 5. 所有上下文都添加到`ServletContext`中。 6. 您可以使用`WebApplicationContextUtils`類訪問根上下文。 ## 總結 通常,您將在`DispatcherServlet`上下文中定義所有與 MVC 相關的 bean(控制器和視圖等),并在`ContextLoaderListener`的根上下文中定義所有跨領域的 bean,例如安全性,事務,服務等。 通常,此設置可以正常工作,因為很少需要訪問任何 MVC bean(從子上下文)到與安全相關的類(從根上下文)。 通常,我們在 MVC 類上使用安全 bean,并且他們可以通過上述設置來訪問它。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看