gpt4 book ai didi

java - 如何禁用 Servlet 3.0 扫描和自动加载组件

转载 作者:行者123 更新时间:2023-11-28 21:54:15 25 4
gpt4 key购买 nike

我们有一个应用程序,它不断从我们的第 3 方库中加载 ServletContainerInitializer 的实例。

一个实例是 JerseyServletContainerInitializer,另一个是 SpringServletContainerInitializer。这些来自 Jersey 和 Spring 的类似乎“接管”了我们的 servlet 上下文,扰乱了我们的映射和过滤器等。

我们确实需要显式配置我们的 servlet 容器的 web.xml,这种自动扫描让我们抓狂。通过简单地在我们的 pom.xml 中引入依赖项,我们的运行时 ServletContext 配置(例如 Servlets/Filters/ContextListeners)会发生变化,因为 servlet 容器会在类路径中找到这些库。

有没有办法使用 Servlet 3 但禁用其烦人的自动类路径扫描“功能”?

最佳答案

来自 https://wiki.apache.org/tomcat/HowTo/FasterStartUp

There are two options that can be specified in your WEB-INF/web.xml file:

  • Set metadata-complete="true" attribute on the <web-app> element.
  • Add an empty <absolute-ordering /> element.

Setting metadata-complete="true" disables scanning your web application and its libraries for classes that use annotations to define components of a web application (Servlets etc.). The metadata-complete option is not enough to disable all of annotation scanning. If there is a SCI with a @HandlesTypes annotation, Tomcat has to scan your application for classes that use annotations or interfaces specified in that annotation.

The <absolute-ordering> element specifies which web fragment JARs (according to the names in their WEB-INF/web-fragment.xml files) have to be scanned for SCIs, fragments and annotations. An empty element configures that none are to be scanned.

In Tomcat 7 the absolute-ordering option affects discovery both of SCIs provided by web application and ones provided by the container (i.e. by the libraries in $CATALINA_HOME/lib). In Tomcat 8 the option affects the web application ones only, while the container-provided SCIs are always discovered, regardless of absolute-ordering. In such case the absolute-ordering option alone does not prevent scanning for annotations, but the list of JARs to be scanned will be empty, and thus the scanning will complete quickly. The classes in WEB-INF/classes are always scanned regardless of absolute-ordering.

Scanning for web application resources and TLD scanning are not affected by these options.

关于java - 如何禁用 Servlet 3.0 扫描和自动加载组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com