如果我们有多个 XML 配置文件,DispatcherServlet 如何工作? [英] How does DispatcherServlet work if we have multiple XML Configuration file?

查看:25
本文介绍了如果我们有多个 XML 配置文件,DispatcherServlet 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如果我们有多个 XML 配置文件,DispatcherServlet 如何工作,那么 Spring 应用程序上下文如何加载它们并对其进行操作?

场景:

就我而言,我们有一个应用程序应该是全球性的,即应用程序应该具有 AP{Asia-Pacific}、EM{Europ-Middleeast}、CA{Canada} 和 LA{Latin America} 版本.

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.

目前,我们有一个区域的应用程序,它是 EM,它有它的 XML 配置文件,即 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.

Currently, we have Application for one region that is EMand its has its XML Configuration File i.e, em-servelt.xml and then there is generic web.xml file now for AP region we have another ap-servlet.xml file and by the way both em-servlet.xml and ap-servlet.xml file would 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.DomainController and ap would be pointing to com.ap.DomainController.

所以我的问题是

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

我希望能够清楚地陈述我的问题.

I hope am able to clearly state my question.

推荐答案

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

The web.xml file can configure multiple DispatcherServlet instances, each having its own configuration. Each DispatcherServlet instance configures a WebApplicationContext separate from other DispatcherServlet instances, so you can use the same bean names without affecting the other application context.

<!-- 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>

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

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

<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>

这篇关于如果我们有多个 XML 配置文件,DispatcherServlet 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆