[英]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>
標記內。 ServletContext
接口的public String getInitParameter(String name)
方法來獲取值。 getServletContext()
方法返回ServletContext的對象。 getServletContext()
方法返回ServletContext的對象。 ServletContext application=getServletConfig().getServletContext();
ServletContext application=getServletContext();
<init-param> .
<Servlet>
標簽內。 ServletConfig
接口的public String getInitParameter(String name)
方法來獲取值。 getServletConfig()
方法返回ServletConfig的對象。 ServletConfig config=getServletConfig();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.