Java 如何在一个 Spring 应用程序中的 web.xml 中注册多个 servlet

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

How to register multiple servlets in web.xml in one Spring application

javaspringservletsweb.xml

提问by David Buckley

I want to define two servlets in my Spring web.xml - one for the application html/jsp pages, and one for a web service that will be called by an external application. Here is the web.xml:

我想在我的 Spring web.xml 中定义两个 servlet - 一个用于应用程序 html/jsp 页面,另一个用于将由外部应用程序调用的 Web 服务。这是 web.xml:

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/user-service-servlet.xml</param-value>
</context-param>

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>user-webservice</servlet-name>
  <url-pattern>/UserService/*</url-pattern>
</servlet-mapping>

If I have myservlet use the DispatcherServlet in the file by itself, it works fine. If I have the user-webservice with the context-param for it's config file (user-service-servlet.xml), it works fine. However, if I have both in the file, then the myservlet doesn't work as the myservlet-servlet.xml file isn't loaded automatically. If I remove the context-param, then the myservlet works, but the user-webservice doesn't work as it's configuration file (user-service-servlet.xml) isn't loaded.

如果我让 myservlet 单独使用文件中的 DispatcherServlet,它就可以正常工作。如果我有 user-webservice 及其配置文件 (user-service-servlet.xml) 的上下文参数,它工作正常。但是,如果我在文件中同时拥有这两个文件,则 myservlet 无法工作,因为 myservlet-servlet.xml 文件不会自动加载。如果我删除上下文参数,则 myservlet 可以工作,但 user-webservice 无法工作,因为它的配置文件 (user-service-servlet.xml) 未加载。

How can I have both servlets defined and both of their configuration files loaded?

如何定义两个 servlet 并加载它们的两个配置文件?

采纳答案by Pascal Thivent

As explained in this threadon the cxf-user mailing list, rather than having the CXFServlet load its own spring context from user-webservice-servlet.xml, you can just load the whole lot into the root context. Rename your existing user-webservice-servlet.xmlto some other name (e.g. user-webservice-beans.xml) then change your contextConfigLocationparameter to something like:

正如在cxf-user 邮件列表上的这个线程中所解释的,与其让 CXFServlet 从 加载它自己的 spring 上下文user-webservice-servlet.xml,您只需将全部加载到根上下文中即可。将您的现有user-webservice-servlet.xml名称重命名为其他名称(例如user-webservice-beans.xml),然后将您的contextConfigLocation参数更改为:

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/applicationContext.xml
    /WEB-INF/user-webservice-beans.xml
  </param-value>
</context-param>

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>user-webservice</servlet-name>
  <url-pattern>/UserService/*</url-pattern>
</servlet-mapping>

回答by cletus

Use config something like this:

使用这样的配置:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>

and then you'll need three files:

然后你需要三个文件:

  • applicationContext.xml;
  • myservlet-servlet.xml; and
  • user-webservice-servlet.xml.
  • applicationContext.xml;
  • myservlet-servlet.xml; 和
  • 用户 webservice-servlet.xml。

The *-servlet.xmlfiles are used automatically and each creates an application context for that servlet.

这些*-servlet.xml文件被自动使用,每个文件都为该 servlet 创建一个应用程序上下文。

From the Spring documentation, 13.2. The DispatcherServlet:

来自 Spring 文档,13.2。DispatcherServlet

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xmlin the WEB-INFdirectory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).

该框架将上的初始化DispatcherServlet寻找名为[servlet的名称] -servlet.xml后缀的文件WEB-INF你的web应用程序的目录,并创建豆定义那里(会覆盖在相同的名称定义的任何豆的定义全局范围)。

回答by Tony Shih

I know this is a bit old but the answer in short would be <load-on-startup> both occurrences have given the same id which is 1 twice. This may confuse loading sequence.

我知道这有点旧,但简而言之,答案是 <load-on-startup> 两次出现都给出了相同的 id,即 1 两次。这可能会混淆加载顺序。