Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring DispatcherServlet Multiple Instances

Tags:

java

spring

Ok Spring Gurus...break this one down for me. While reading the documentation on Spring MVC, I cam e across this statement:

"....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 Docs Reference

I have a few questions based on this:

  1. The statement implies that there can be more than one instance of DispatcherServlet. Is that totally NOT true because in a web app, only one instance of a Servlet can (and should ideally) exists?
  2. What exactly does "servlet-specific scope" mean in the context of the above statement?
like image 891
mainas Avatar asked Nov 04 '22 16:11

mainas


1 Answers

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

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.

It's true that, except in some limited cases, the container is allowed to only instantiate one instance of the class per servlet. However, you can have multiple <servlet> entries that all specify the same class.

What exactly does "servlet-specific scope" mean in the context of the above statement

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. You can define your business objects in the root config, and the datasource in the web-app config. As well as any client-specific configuration beans.

like image 92
zippy Avatar answered Nov 15 '22 00:11

zippy