簡體   English   中英

在web.xml中加載上下文

[英]Loading context in web.xml

在上下文參數中加載上下文並在Dispatcher Servlet的init-param中加載它有什么區別。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

VS

<init-param>    
        <param-name>contextConfigLocation</param-name>    
        <param-value> /WEB-INF/mvc-config.xml </param-value>    
  </init-param> 

我所理解的是context-param由上下文監聽器加載,並且應該只包含中間層bean。 其中init方法中的Dispatcher Servlet應該加載Web層bean。 這種理解是否正確? 為什么我們單獨加載2件東西?

在context-param“contextConfigLocation”中,您應該包括您的應用程序上下文,因為您已經說過中間層bean,例如:services,datasource ...

Spring DispatcherServlet將在WEB-INF / servletName-servlet.xml中查找配置文件。 使用init-param可以更改此默認行為。 servlet上下文(Web上下文)是隔離的,但可能將應用程序上下文保持為父級。 您可以單獨使用它們中的一個或其中一個。

<context-param>
  • 寫在<Servlet>標記之外,位於<webapp>標記內。
  • 十進制值將可用於整個應用程序
  • 應用程序中的任何servlet(在web.xml中聲明)都可以訪問這些值
  • 因此,當我們想要在應用程序中跨servlet共享同一組值時,我們使用它,例如數據庫配置詳細信息。
  • 您可以使用ServletContext接口的public String getInitParameter(String name)方法來獲取值。
  • ServletConfig接口的getServletContext()方法返回ServletContext的對象。
  • GenericServlet類的getServletContext()方法返回ServletContext的對象。
  • 示例1: ServletContext application=getServletConfig().getServletContext();
  • 示例2: ServletContext application=getServletContext();

<init-param> .

  • 寫在<Servlet>標簽內。
  • 聲明的值僅對servlet可用。
  • 您可以使用ServletConfig接口的public String getInitParameter(String name)方法來獲取值。
  • Servlet接口的getServletConfig()方法返回ServletConfig的對象。
  • 示例: ServletConfig config=getServletConfig();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM