编程语言
首页 > 编程语言> > java-如何禁用Servlet 3.0扫描和自动加载组件

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

作者:互联网

我们有一个应用程序,该应用程序不断从我们的第三方库中加载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.

标签:servlet-3-0,java,spring,tomcat,servlets
来源: https://codeday.me/bug/20191011/1891001.html