java 如果我们有多个 XML 配置文件,DispatcherServlet 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4054896/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:35:37  来源:igfitidea点击:

How does DispatcherServlet work if we have multiple XML Configuration file?

javaspringarchitecture

提问by Rachel

Questions

问题

How does DispatcherServlet work if we have multiple XML Configuration file so how does Spring Application Context loads them and acts on them ?

如果我们有多个 XML 配置文件,那么 DispatcherServlet 如何工作,那么 Spring Application Context 如何加载它们并对其进行操作?

Scenario:

设想:

In my case, we have an application that is supposed to global that is application should have AP{Asia-Pacific}, EM{Europ-Middleeast}, CA{Canada} and LA{Latin America}Versions.

就我而言,我们有一个应用程序应该是全局的,即应用程序应该有AP{Asia-Pacific}, EM{Europ-Middleeast}, CA{Canada} and LA{Latin America}版本。

Currently, we have Application for one region that is EMand its has its XML Configuration File i.e, em-servelt.xmland then there is generic web.xmlfile now for AP regionwe have another ap-servlet.xmlfile and by the way both em-servlet.xmland ap-servlet.xml filewould have same bean names but they would be pointing to Controllers in different packages, so for example, em would be pointing to something like com.em.DomainControllerand ap would be pointing to com.ap.DomainController.

目前,我们有这是一个地区的应用EM和它的拥有XML Configuration File i.e, em-servelt.xml,再有就是通用的web.xml文件现在AP region,我们有另一个ap-servlet.xml文件和方式都em-servlet.xmlap-servlet.xml file将有相同的bean的名字,但他们会指向不同封装控制器,所以例如, em 将指向类似的东西,com.em.DomainController而 ap 将指向com.ap.DomainController.

So my question is

所以我的问题是

How is the request mapped to different controllers and how request is being recognized so that it should read from ap-servlet.xml or em-servlet.xml ?

请求如何映射到不同的控制器以及如何识别请求以便它应该从 ap-servlet.xml 或 em-servlet.xml 读取?

I hope am able to clearly state my question.

我希望能够清楚地说明我的问题。

回答by Chin Huang

The web.xmlfile can configure multiple DispatcherServletinstances, each having its own configuration. Each DispatcherServletinstance configures a WebApplicationContextseparate from other DispatcherServletinstances, so you can use the same bean names without affecting the other application context.

web.xml文件可以配置多个DispatcherServlet实例,每个实例都有自己的配置。每个DispatcherServlet实例配置一个WebApplicationContext独立于其他DispatcherServlet实例的实例,因此您可以使用相同的 bean 名称而不会影响其他应用程序上下文。

<!-- configured by WEB-INF/ap-servlet.xml -->
<servlet>
    <servlet-name>ap</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- configured by WEB-INF/em-servlet.xml -->
<servlet>
    <servlet-name>em</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

You must also configure web.xmlto map requests to the appropriate DispatcherServlet. For example, each region could have a different URL path.

您还必须配置web.xml以将请求映射到适当的DispatcherServlet. 例如,每个区域可以有不同的 URL 路径。

<servlet-mapping>
    <servlet-name>ap</servlet-name>
    <url-pattern>/ap/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>em</servlet-name>
    <url-pattern>/em/*</url-pattern>
</servlet-mapping>

回答by matt b

The web.xmlfile controls which context file DispatcherServlet is using. If you configure web.xmlto have a DispatcherServletwith name em, then by default it uses em-servlet.xmlto load the web context.

web.xml文件控制 DispatcherServlet 正在使用的上下文文件。如果您配置web.xmlDispatcherServletwith name em,则默认情况下它用于em-servlet.xml加载 Web 上下文。

Your question is a bit confusing as to what you would actually like to do - do you want all "versions" to be available in the same instance of the application?

您的问题对于您实际想要做什么有点令人困惑 - 您是否希望所有“版本”都在应用程序的同一实例中可用?

If so, the method you describe sounds unorthodox for how to present multiple languages / globalizing your application. Traditionally you would just have a single instance of the application and all the controllers/instances, and then handle translating user-visible messages at the display level. Spring has excellent support for this.

如果是这样,您描述的方法对于如何呈现多种语言/全球化您的应用程序来说听起来很不正统。传统上,您只有一个应用程序实例和所有控制器/实例,然后在显示级别处理转换用户可见的消息。Spring对此有很好的支持。

If your goal is to have a single instance of the application serve requests for all these languages/locales, then it sounds like you could do away with a lot of this redundancy.

如果您的目标是让应用程序的单个实例为所有这些语言/区域设置服务请求,那么听起来您可以消除很多这种冗余。