spring 我们可以更改 DispatcherServlet 上下文配置文件名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3744520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we change DispatcherServlet context configuration file name?
提问by javanoob
In Spring web mvc
在 Spring web mvc 中
1) If we define DispatcherServletas below
1)如果我们定义DispatcherServlet如下
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
it looks for file named appServlet-servlet.xmlunder WEB-INFfolder as mentioned in the spring reference.
它查找在 spring 参考中提到的文件夹appServlet-servlet.xml下命名的WEB-INF文件。
My question is can we change this file nameand locationit looks for? (I think using context or init parameters we can do this,can any body tell me what exactly it should be?)
我的问题是我们可以更改它查找的文件名和位置吗?(我认为使用上下文或初始化参数我们可以做到这一点,任何人都可以告诉我它到底应该是什么吗?)
2) In every spring web mvc web.xml,we will have the below line:
2)在每个spring web mvc中web.xml,我们都会有以下一行:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Here, My question is what context files it looks for? (is is this context loader listener which looks for dispatcherservlet-servlet.xml?)
在这里,我的问题是它寻找什么上下文文件?(这是查找 dispatcherservlet-servlet.xml 的上下文加载器侦听器吗?)
3) Difference between dispatcherservlet-servlet.xmland applicationcontext.xml? (I saw some examples..where people are importing applicationcontext.xmlinto dispatcherservlet-servlet.xml?)
3)dispatcherservlet-servlet.xml和之间的区别applicationcontext.xml?(我看到一些examples..where人导入applicationcontext.xml到dispatcherservlet-servlet.xml?)
4) Please tell me how many contexts we can have for spring web and are there any naming conventions for this(like dispatcher servlet)?/
4) 请告诉我 Spring Web 可以有多少个上下文,并且是否有任何命名约定(例如调度程序 servlet)?/
回答by Bozho
"The namespace can also be set explicitly via the
namespaceservlet init-param." You can set whatever path you want there, relative to the context rootNo, the
ContextLoaderListenerlooks forapplicationContext.xml(or for the file specified by the context-paramcontextConfigLocation. Again the path is relative to the context-root. I usually place mine in/WEB-INF/classes/applicationContext.xml, and set this as a value of the context-param).The
dispatcherServlet-servlet.xmlis a child context to the one defined byapplicationContext.xml. The child context can access beans from the parent context, but the opposite is not true. So imagine you have a "web" context, with all controllers and web-related stuff, and a "main" context with everything elseIt is advisable to have as few contexts as possible (for the sake of simplicity). But you can define multiple dispatcher servlets, and hence have multiple "child" contexts.
“命名空间也可以通过
namespaceservlet init-param显式设置。” 你可以在那里设置你想要的任何路径,相对于上下文根不,
ContextLoaderListener寻找applicationContext.xml(或由 context-param 指定的文件contextConfigLocation。同样,路径是相对于 context-root 的。我通常将我的放在 中/WEB-INF/classes/applicationContext.xml,并将其设置为 context-param 的值)。该
dispatcherServlet-servlet.xml是孩子上下文的定义的applicationContext.xml。子上下文可以从父上下文访问 bean,但反之则不然。所以想象一下你有一个“web”上下文,包含所有控制器和与 web 相关的东西,以及一个包含其他所有内容的“主”上下文建议使用尽可能少的上下文(为了简单起见)。但是您可以定义多个调度程序 servlet,因此有多个“子”上下文。
回答by dira
My question is can we change this file name and location it looks for?
我的问题是我们可以更改它查找的文件名和位置吗?
Did you mean that you want to use a file which is NOT named as appServlet.xml
您的意思是要使用未命名为的文件 appServlet.xml
Copying-pasting from mvc-shocase/web.xml
从mvc-shocase/web.xml复制粘贴
<!-- 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>
回答by Jagadeesh Keerthi
1A)Yes, but we need to pass the init-param to the DispatcherServlet with name and value as "contextConfigLocation" and "location of ur file" respectively in DD or web.xml file.
1A)是的,但我们需要将 init-param 传递给 DispatcherServlet,名称和值分别为 DD 或 web.xml 文件中的“contextConfigLocation”和“您的文件的位置”。
2A)Bozho already answered. It[the ContextLoaderListener] looks to load the context xml file(s) provided as context param-value mapped to context param-name for the context-param. Looking for the "dispatcherservlet-servlet.xml" is a default process in Spring MVC. ContextLoaderListener don't looks for it.
2A)Bozho 已经回答了。它[ContextLoaderListener] 看起来加载作为上下文参数值映射到上下文参数的上下文参数名称提供的上下文xml 文件。寻找“dispatcherservlet-servlet.xml”是Spring MVC中的一个默认过程。ContextLoaderListener 不查找它。
3A)Bozho already answered.
3A)Bozho 已经回答了。
4A)Bozho already answered.
4A)Bozho 已经回答了。

