java - 在 spring mvc 中使用多个调度程序 servlet 的优点和缺点

标签 java spring spring-boot spring-mvc servlets

我刚刚发现您可以在 Spring 应用程序中拥有多个 dispatcher-servlet。我想知道采用这种方法是否比传统的 spring 方法有任何优势,为所有传入请求使用 single dispatcher-servlet

最佳答案

来自文档

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.

多个调度程序 servlet 的优点 为什么我们需要多个调度程序 servlet?

简单的答案是 DispatcherServlet's多种形式的功能

调度程序 servlet 功能

<小时/>

<小时/> 我将尝试解释 DispatcherServlet

提供的一些功能

声明多个调度程序 servlet
考虑我们有两个调度程序 servlet (DS),其中 DS1、DS2 配置了不同的 url 模式(**.simple, **.beanName),并且它们使用下面提供的不同调度程序 servlet 配置。

DispatcherServlet     - simpleUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/simpleUrlHandlerMapping.xml
<url-pattern>*.simple</url-pattern>

DispatcherServlet     - beanNameUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/beanNameUrlHandlerMapping.xml
<url-pattern>*.beanName</url-pattern>

优点1:我们可以为不同的URL集设置不同的HandlerMapping

DS1 bean 名称 url 处理程序映射配置

<bean name="/hello.beanName" class="com.pvn.mvc.HelloController" />
<bean name="/hi.beanName" class="com.pvn.mvc.HiController" />

DS2 简单 URL 处理程序映射配置

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/hello.simple">simpleHello</prop>
            <prop key="/hi.simple">simpleHi</prop>
        </props>
    </property>
</bean>

优点 2: 我们可以为不同的 URL 集使用不同的 View 解析器。

DS1 的InternalResourceViewResolver
它仅处理前缀+返回的字符串+后缀
<强> TilesViewResolver DS2
它的实现由 apachetiles 提供,这是一个基于布局/骨架的插件高级功能,如下所示。
enter image description here 或者,如果我们对不同的 URL 集使用不同布局的 TilesViewResolver
匿名用户 - 不同的布局
登录用户 - 不同的布局

优点3:我们可以为不同的URL集使用不同的主题解析器。
这些解析器持续监视 cookie/ session 来解析主题并提供合格的样式表/主题(如下图所示)。下面仅给出 CookieThemeResolver 结果的示例.
注意:这不是关于主题配置,而是关于主题解析器配置。

enter image description here

优点 4: 我们可以为不同的 URL 集使用不同的区域设置解析器。
这些解析器持续监视 cookie/session/accept-header 以解析区域设置并加载合格的应用程序消息(如下图所示)。下面仅给出 CookieLocaleResolver 结果的示例.
注意:这不是关于区域设置配置,而是关于区域设置解析器配置。
enter image description here

关于java - 在 spring mvc 中使用多个调度程序 servlet 的优点和缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58149539/

相关文章:

java - 使用 static 关键字覆盖实例方法

spring - 容器管理安全、Spring Security 和身份验证

spring - 是否可以在集成测试期间禁用 Spring 的 @Async?

java - 使用 Logback RollingFileAppender 时,有没有办法使用默认的 Spring Boot "/actuator/logfile"端点?

java - MVC : Should the Model layer only contain DAO and DAOHelpers?

java - 在 GlassFish Server Open Source Edition 3.1.2 中管理共享库

java - "Cannot find symbol"- 具有 main 方法的类可以从其他类之一调用方法,但不能从其他第二个类调用方法?

java - 为什么响应实体返回带有 setter/getter 名称而不是数据成员名称的 json

java - Spring RequestContextHolder 和 WebTestClient

spring-boot - Spring Boot 中带有 DSL 的 Apache Camel Rest 端点将/camel 添加到路径中