如何禁用Servlet 3.0扫描和自动加载组件 [英] How to disable Servlet 3.0 scanning and auto loading of components

查看:302
本文介绍了如何禁用Servlet 3.0扫描和自动加载组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序,该应用程序不断从我们的第三方库中加载ServletContainerInitializer的实例.

We have an application which keeps loading instances of ServletContainerInitializer from our 3rd party libs.

一个实例是JerseyServletContainerInitializer,另一个实例是SpringServletContainerInitializer.这些来自Jersey和Spring的类似乎接管"了我们的servlet上下文,使我们的映射和过滤器等一团糟.

One instance is JerseyServletContainerInitializer and the other is SpringServletContainerInitializer. These classes from Jersey and Spring seem to "take over" our servlet context messing with our mappings and filters and such.

我们确实需要显式配置servlet容器的web.xml,并且这种自动扫描使我们发疯.通过简单地在pom.xml中引入依赖关系,可以更改运行时ServletContext配置(例如Servlets/Filters/ContextListeners),因为Servlet容器在类路径中找到了这些库.

We really need to explicitly configure our servlet container's web.xml and this auto scanning is driving us insane. By simply pulling in a dependency in our pom.xml our runtime ServletContext configurations such as Servlets/Filters/ContextListeners are mutated because the servlet container finds these libraries on the classpath.

是否可以使用Servlet 3,但禁用其烦人的自动类路径扫描功能"?

Is there a way to use Servlet 3 but disable its annoying auto classpath scanning "feature"?

推荐答案

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

您可以在WEB-INF/web.xml中指定两个选项 文件:

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

  • 在< web-app>元素上设置metadata-complete ="true"属性.
  • 添加一个空的< absolute-ordering/>元素.

设置metadata-complete ="true"将禁用对您的网络的扫描 应用程序及其使用注解的类的库 定义Web应用程序的组件(Servlet等).这 元数据完整选项不足以禁用所有注释 扫描.如果存在带有@HandlesTypes批注的SCI,则为Tomcat 必须扫描您的应用程序以查找使用批注或 该注释中指定的接口.

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.

< absolute-ordering>元素指定哪个Web片段JAR (根据其WEB-INF/web-fragment.xml文件中的名称)具有 进行扫描以查找SCI,片段和注释.一个空的 元素配置为不扫描任何内容.

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.

在Tomcat 7中,绝对顺序选项会影响以下两个方面的发现: Web应用程序提供的SCI和容器提供的SCI (即$ CATALINA_HOME/lib中的库).在Tomcat 8中,该选项 仅影响Web应用程序,而容器提供 不论绝对顺序如何,总是会发现SCI.在这样的 单独使用绝对排序选项不会阻止扫描的情况 批注,但是要扫描的JAR列表将为空,并且 因此扫描将很快完成.中的课程 始终扫描WEB-INF/class,而不考虑绝对顺序.

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.

不扫描Web应用程序资源和TLD扫描 受这些选项的影响.

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

这篇关于如何禁用Servlet 3.0扫描和自动加载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆