简体   繁体   English

Spring DispatcherServlet多个实例

[英]Spring DispatcherServlet Multiple Instances

Ok Spring Gurus...break this one down for me. 好的春天大师......为我打破这个。 While reading the documentation on Spring MVC, I cam e across this statement: 在阅读有关Spring MVC的文档时,我想到了这个声明:

"....ApplicationContext instances in Spring can be scoped. In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans defined can be overridden in the servlet-specific scope, and new scope-specific beans can be defined local to a given servlet instance." “.... Spring中的ApplicationContext实例可以作用域。在Web MVC框架中,每个DispatcherServlet都有自己的WebApplicationContext,它继承了已经在根WebApplicationContext中定义的所有bean。这些继承的bean可以在特定于servlet中被覆盖范围,并且可以在给定servlet实例的本地定义新的特定于范围的bean。“

Spring Docs Reference Spring Docs参考

I have a few questions based on this: 基于此,我有几个问题:

  1. The statement implies that there can be more than one instance of DispatcherServlet. 该声明暗示可以有多个DispatcherServlet实例。 Is that totally NOT true because in a web app, only one instance of a Servlet can (and should ideally) exists? 这完全不正确,因为在Web应用程序中,只有一个Servlet实例可以(并且理想情况下)存在吗?
  2. What exactly does "servlet-specific scope" mean in the context of the above statement? 在上述声明的上下文中,“servlet特定范围”到底意味着什么?

Is that not totally NOT true 这不完全不正确

This translates to "occasionally true," which I don't think is what you meant. 这转化为“偶尔是真的”,我认为这不是你的意思。

in a web app, only one instance of a Servlet can (and should ideally) exists 在Web应用程序中,只有一个Servlet实例可以(并且理想情况下)存在

This is not totally incorrect -- just mostly incorrect. 这并非完全错误 - 只是大多数不正确。 You are confusing the <servlet> element of the deployment descriptor ( web.xml ) with the class implementing that servlet. 您将部署描述符( web.xml )的<servlet>元素与实现该servlet的类混淆。

It's true that, except in some limited cases, the container is allowed to only instantiate one instance of the class per servlet . 确实,除了在某些有限的情况下,允许容器仅为每个servlet实例化一个类的实例。 However, you can have multiple <servlet> entries that all specify the same class. 但是,您可以拥有多个<servlet>条目,这些条目都指定了相同的类。

What exactly does "servlet-specific scope" mean in the context of the above statement 在上述声明的上下文中,“servlet特定范围”究竟是什么意思

As a not-very-good example (indeed, I think there are few good examples): you might have an application that supports multiple clients, each of whom have different URLs, and different databases, but are otherwise identical. 作为一个不太好的例子(事实上,我认为没有很好的例子):你可能有一个支持多个客户端的应用程序,每个客户端都有不同的URL和不同的数据库,但在其他方面是相同的。 You can define your business objects in the root config, and the datasource in the web-app config. 您可以在根配置中定义业务对象,也可以在web-app配置中定义数据源。 As well as any client-specific configuration beans. 以及任何特定于客户端的配置bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM